C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

how to save to a text file

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
mov
Guest





PostPosted: Thu Oct 28, 2004 5:29 pm    Post subject: how to save to a text file Reply with quote



I am a newbie, please someone tell me how to create a text file and save it
using c++.
thanks


Back to top
Victor Bazarov
Guest





PostPosted: Thu Oct 28, 2004 5:37 pm    Post subject: Re: how to save to a text file Reply with quote



mov wrote:
Quote:
I am a newbie, please someone tell me how to create a text file and save it
using c++.

Create an std::ofstream object. Write to it using <<.

#include int main()
{
std::ofstream os("text_file.txt");
os << "Hello world!";
}

V

Back to top
Aggro
Guest





PostPosted: Thu Oct 28, 2004 7:21 pm    Post subject: Re: how to save to a text file Reply with quote



Victor Bazarov wrote:

Quote:
#include int main()
{
std::ofstream os("text_file.txt");

// Check could we open file for writing or not
if( os )
{
os << "Hello world!";
}
else
{
// This could happen for example if file already exists
// and it is read-only or user doesn't have permission to write
// into it.
std::cout << "Failed to open file";
}

Quote:
}


Back to top
Victor Bazarov
Guest





PostPosted: Thu Oct 28, 2004 7:28 pm    Post subject: Re: how to save to a text file Reply with quote

Aggro wrote:
Quote:
Victor Bazarov wrote:

#include int main()
{
std::ofstream os("text_file.txt");


// Check could we open file for writing or not
if( os )
{
os << "Hello world!";
}
else
{
// This could happen for example if file already exists
// and it is read-only or user doesn't have permission to write
// into it.
std::cout << "Failed to open file";

I'd actually output that to std::cerr ...

Quote:
}

}



V

Back to top
Karthik Kumar
Guest





PostPosted: Sun Oct 31, 2004 3:08 am    Post subject: Re: how to save to a text file Reply with quote

Victor Bazarov wrote:
Quote:
mov wrote:

I am a newbie, please someone tell me how to create a text file and
save it using c++.


Create an std::ofstream object. Write to it using <<.

#include int main()
{
std::ofstream os("text_file.txt");
os << "Hello world!";
}

V

Isn't it important to close the file after writing it ?

os.close(); ?


--
Karthik. http://akktech.blogspot.com .
'Remove _nospamplz from my email to mail me.'

Back to top
John Harrison
Guest





PostPosted: Sun Oct 31, 2004 9:42 am    Post subject: Re: how to save to a text file Reply with quote

Quote:

Isn't it important to close the file after writing it ?

os.close(); ?


NO!!

This is C++, the ofstream destructor does that for you. This does seem to be
a very common misunderstanding however. I guess programmers get it drilled
into them from an early age that you must always close files that no-one
imagines that it could be any different in C++.

john



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.