Page 1 of 1

fstream C++ Programming Question

Posted: Tue Sep 07, 2010 6:56 pm
by AbsoluteCPP

Code: Select all

std::fstream *fs = NULL;
	fs = new std::fstream();
	fs->open("D:/Projects/Test_project/Release/test.txt");

	if(!fs)
		cout << "\nUnable to Open";

	*fs << "Put this text in file";

	fs->close();
why this code not work, (it compiles successfully) but create no txt file as a output.

Re: fstream C++ Programming Question

Posted: Tue Sep 07, 2010 11:19 pm
by randomMesh
AbsoluteCPP wrote:

Code: Select all

std::fstream *fs = NULL;
	fs = new std::fstream();
	fs->open("D:/Projects/Test_project/Release/test.txt");

	if(!fs)
		cout << "\nUnable to Open";

	*fs << "Put this text in file";

	fs->close();
why this code not work, (it compiles successfully) but create no txt file as a output.
Better use something like this

Code: Select all

std::ofstream file("file.txt", std::ios::out);
if (file.good())
{
   file << "Put this text in file\n";
}
else
{
   //Error handling here
}

file.close();