/* program demonstrating simple output to a file * * it just reads lines in from the user and writes them to a file */ #include #include #include using namespace std; int main() { // declare the stream object ofstream fout; // connect it to an external file fout.open("notes.txt"); // start the loop string s; cout << "just enter some lines, hit by itself to quit.\n\n"; do { // get a line from the user getline(cin, s,'\n'); // send it to the file fout << s << endl; } while (s != ""); // shut down the stream (important!) fout.close(); } /* note: we could combine the fout declaration with the call to open() by just using: ofstream fout("notes.txt"); note2: it is usually better to declare a string and ask the user for the file name rather than encoding it directly. This would allow for output file names other than "notes.txt". */