weekend get bored
#include
#include
#include
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string filename="test.txt";
string line="hello worldrn";
fstream ofile(filename.c_str(),
ios_base::out | ios_base::binary);
if(!ofile.is_open())
{
cout << "Cannot Open " << filename.c_str()
<< " for writingn";
return 0;
}
for(int i=0; i<5; i++) //write 4 lines to file
ofile.write(line.c_str(), line.length());
ofile.close();
return 0;
}
and read text file
char FileBuffer[256];
fstream ifile(filename.c_str(),
ios_base::in | ios_base::binary);
if(!ifile.is_open())
{
cout << "Cannot Open " << filename.c_str()
<< " for readingn";
return 0;
}
while(!ifile.eof())
{
ifile.getline(FileBuffer, 256);
cout << FileBuffer << endl;
}
ifile.close();
seriously I dont use fstream in any of my programm, I use fopen,fread and fwrite. fstream for newbie to c++ 😉