#include #include int main () { char s[100]; ofstream TestOutput ("output.data", ios :: out); ifstream TestInput ("input.data", ios :: in); if (!TestOutput) cout << "output.data not attached\n"; if (!TestInput) cout << "input.data not attached\n"; cout << "Enter a string: "; TestOutput << "Enter a string: "; while (true) { cin.getline (s, 100, '\n'); TestInput.getline (s, 100, '\n'); if (TestInput.eof()) { cout << "end of file" << endl; TestOutput << "end of file" << endl; TestOutput.close(); return 0; }; cout << "You entered: \"" << s << "\"" << endl; cout << "next string: "; TestOutput << "You entered: \"" << s << "\"" << endl; TestOutput << "next string: "; TestOutput.flush(); } cout << "End of File" << endl; TestOutput << "End of File" << endl; TestOutput.close(); return 0; } /* Enter a string: This a test You entered: "This is a string" next string: And another You entered: "And this is another" next string: // a CR on standard input You entered: "" next string: // another CR on standard input end of file // should have found EOF on input.data sooner */ /* input.data This is a string And this is another */ /* output.data Enter a string: You entered: "This is a string" next string: You entered: "And this is another" next string: You entered: "" // not so on input.data next string: end of file // finally found the EOF */ /* This example illustrates that the structure of a file can be: First Line\n Other lines\n Last Line\n \nEOF Clearly you have to watch for this, since it is possible to construct files without two \n\n at the end. That is, finishing Last Line\nEOF */