[   ^ to index...   |   <-- previous   ]

File streams

As noted, cin, cout, and cerr are not the only streams (though they are the only statically constructed standard stream objects). The other kind of I/O streams that you're probably most interested in are file streams:

#include <fstream.h> int main() { // Open file streams. Notice use of constructors ifstream in("in.txt"); ofstream out("out.txt"); char word[200]; // Note use of error checking. if (!in) { cout << "Unable to open input file." << endl; return 1; } if (!out) { cout << "Unable to open output file." << endl; return 1; } // Can you guess what the following does? while (in >> word) out << word << '\n'; // If we did not call close() explicitly, the destructors // would perform the close automatically at the end of // this function. in.close(); out.close(); }

Last modified: Wed Jun 28 20:21:58 PDT 2000