| View previous topic :: View next topic |
| Author |
Message |
mov Guest
|
Posted: Thu Oct 28, 2004 5:29 pm Post subject: how to save to a text file |
|
|
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
|
Posted: Thu Oct 28, 2004 5:37 pm Post subject: Re: how to save to a text file |
|
|
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
|
Posted: Thu Oct 28, 2004 7:21 pm Post subject: Re: how to save to a text file |
|
|
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";
}
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Oct 28, 2004 7:28 pm Post subject: Re: how to save to a text file |
|
|
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 ...
V
|
|
| Back to top |
|
 |
Karthik Kumar Guest
|
Posted: Sun Oct 31, 2004 3:08 am Post subject: Re: how to save to a text file |
|
|
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
|
Posted: Sun Oct 31, 2004 9:42 am Post subject: Re: how to save to a text file |
|
|
| 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 |
|
 |
|