fstream problem

L

las

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>

int main()
{
string token1 = "Hello\nall";
fstream newfile("C:/temp/test1.txt", ios::trunc);
if ( newfile.good() )
{
cout << token1.c_str() << endl << "Size of string is " <<
token1.length() << endl;
newfile.write( token1.c_str(), token1.length() );
newfile.flush();
}
else
{
cout << "Error with newfile" << endl;
}

file.close();
system("PAUSE");
return 0;
}

Why doesnt the above code write the token1 string to the test1.txt file ?
 
S

Simon Saunders

las said:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>

int main()
{
string token1 = "Hello\nall";
fstream newfile("C:/temp/test1.txt", ios::trunc);
if ( newfile.good() )
{
cout << token1.c_str() << endl << "Size of string is " <<
token1.length() << endl;
newfile.write( token1.c_str(), token1.length() );
newfile.flush();
}
else
{
cout << "Error with newfile" << endl;
}

file.close();
system("PAUSE");
return 0;
}

Why doesnt the above code write the token1 string to the test1.txt file ?

Try the following instead:

fstream newfile("C:/temp/test1.txt", ios::eek:ut);

or:

ofstream newfile("C:/temp/test1.txt");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top