I compiled a basic file "writing" program from an example in a c++ book and, while it finishes with no errors, nothing happens. The program simply completes with a returning 0. I'm new to file writing so can you point out why exactly it's not writing to the file like I expected? The book shows its result as the command prompt open, as well as poem.txt open with the string poem contained inside. I have a poem.txt located on my desktop; I assume that's where the .txt file has to be. But of course my poem.txt contains nothing.
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
int main()
{
string poem("\n\tI never saw a man who looked");
poem.append("\n\tWith such a wistful eye");
poem.append("\n\tUpon that little tent of blue");
poem.append("Which prisoners call the sky");
ofstream writer("poem.txt");
if (!writer)
{
cout << "Error opening file for output" << endl;
return -1;
}
writer << poem << endl;
writer.close();
cin.get();
return 0;
}