fstream C++ Programming Question

Discussion about everything. New games, 3d math, development tips...
Post Reply
AbsoluteCPP
Posts: 8
Joined: Fri Aug 27, 2010 3:41 pm
Location: Multan, Pakistan

fstream C++ Programming Question

Post 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.
Can't Be Tamed
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: fstream C++ Programming Question

Post 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();
"Whoops..."
Post Reply