#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
fstream tableFile;
tableFile.open("table_file.txt");
if (!tableFile)
{
cout << "\nSorry. There was an error writing the student data to a file.\n\n";
}
return 0;
}
#include <iostream>
#include <fstream>
usingnamespace std;
int main ()
{
fstream fs;
fs.open ("test.txt", fstream::out);
if (!fs)
cout<<"ERROR";
else
{
cout<<"Writing to file...";
fs << "Testing 1...2...3...";
fs.close();
}
return 0;
}
Works for me. It could be that you failed to specify the mode you were opening the file in. If you check the directory where your project is, you should have a test.txt with the correct content after you run this.
Changing line 10 to fs.open ("test.txt", fstream::out | fstream::app); would make you add to the end of the file if it already exists, and create it if it doesn't.