home *** CD-ROM | disk | FTP | other *** search
- //: C18:Iofile.cpp
- // From Thinking in C++, 2nd Edition
- // Available at http://www.BruceEckel.com
- // (c) Bruce Eckel 1999
- // Copyright notice in Copyright.txt
- // Reading & writing one file
- #include "../require.h"
- #include <iostream>
- #include <fstream>
- using namespace std;
-
- int main() {
- ifstream in("Iofile.cpp");
- assure(in, "Iofile.cpp");
- ofstream out("Iofile.out");
- assure(out, "Iofile.out");
- out << in.rdbuf(); // Copy file
- in.close();
- out.close();
- // Open for reading and writing:
- ifstream in2("Iofile.out", ios::in | ios::out);
- assure(in2, "Iofile.out");
- ostream out2(in2.rdbuf());
- cout << in2.rdbuf(); // Print whole file
- out2 << "Where does this end up?";
- out2.seekp(0, ios::beg);
- out2 << "And what about this?";
- in2.seekg(0, ios::beg);
- cout << in2.rdbuf();
- } ///:~
-