home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C18 / Seeking.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  830 b   |  27 lines

  1. //: C18:Seeking.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Seeking in iostreams
  7. #include "../require.h"
  8. #include <iostream>
  9. #include <fstream>
  10. using namespace std;
  11.  
  12. int main(int argc, char* argv[]) {
  13.   requireArgs(argc, 1);
  14.   ifstream in(argv[1]);
  15.   assure(in, argv[1]); // File must already exist
  16.   in.seekg(0, ios::end); // End of file
  17.   streampos sp = in.tellg(); // Size of file
  18.   cout << "file size = " << sp << endl;
  19.   in.seekg(-sp/10, ios::end);
  20.   streampos sp2 = in.tellg();
  21.   in.seekg(0, ios::beg); // Start of file
  22.   cout << in.rdbuf(); // Print whole file
  23.   in.seekg(sp2); // Move to streampos
  24.   // Prints the last 1/10th of the file:
  25.   cout << endl << endl << in.rdbuf() << endl;
  26. } ///:~
  27.