I was trying to use code similar to this in my project and it wasn't working. Seeing as how I've never had any experience with fstream, I figured I should try creating a new project utilizing just fstream (eliminate as many possibilities as I can right?). This simple program compiles and runs fine but does not create the file specified. Any ideas why? I'm using VC++ Express as the compiler.
// writing on a text file
#include <iostream>
#include <fstream>
usingnamespace std;
int main () {
fstream myfile ("example.txt", fstream::in | fstream::out | fstream::app);
if (myfile.is_open())
{
myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
The file will likly be put in the same folder as your code.