Seeking in iostreams

Each type of iostream has a concept of where its “next” character will come from (if it’s an istream) or go (if it’s an ostream). In some situations you may want to move this stream position. You can do it using two models: One uses an absolute location in the stream called the streampos; the second works like the Standard C library functions fseek( )for a file and moves a given number of bytes from the beginning, end, or current position in the file.

The streamposapproach requires that you first call a “tell” function: tellp( )for an ostreamor tellg( )for an istream. (The “p” refers to the “put pointer” and the “g” refers to the “get pointer.”) This function returns a streamposyou can later use in the single-argument version of seekp( )for an ostreamor seekg( )for an istream, when you want to return to that position in the stream.

The second approach is a relative seek and uses overloaded versions of seekp( )and seekg( ). The first argument is the number of bytes to move: it may be positive or negative. The second argument is the seek direction:

ios::beg  From beginning of stream
ios::cur  Current position in stream
ios::end  From end of stream

Seeking.cpp

   1:  #include "../require.h"
   2:  #include <iostream>
   3:  #include <fstream>
   4:  using namespace std;
   5:   
   6:  int main() {
   7:    ifstream in("Seeking.cpp");
   8:    assure(in, "Seeking.cpp"); // File must already exist
   9:    in.seekg(0, ios::end); // End of file
  10:    streampos sp = in.tellg(); // Size of file
  11:    cout << "file size = " << sp << endl;
  12:    in.seekg(-sp/10, ios::end);
  13:    streampos sp2 = in.tellg();
  14:    in.seekg(0, ios::beg); // Start of file
  15:    cout << in.rdbuf(); // Print whole file
  16:    in.seekg(sp2); // Move to streampos
  17:    // Prints the last 1/10th of the file:
  18:    cout << endl << endl << in.rdbuf() << endl;
  19:  } ///:~

This program picks a file name off the command line and opens it as an ifstream. assert( ) detects an open failure. Because this is a type of istream, seekg( )is used to position the “get pointer.” The first call seeks zero bytes off the end of the file, that is, to the end. Because a
streamposis a typedeffor a long, calling tellg( )at that point also returns the size of the file, which is printed out. Then a seek is performed moving the get pointer 1/10 the size of the file – notice it’s a negative seek from the end of the file, so it backs up from the end. If you try to seek positively from the end of the file, the get pointer will just stay at the end. The streamposat that point is captured into sp2, then a seekg( )is performed back to the beginning of the file so the whole thing can be printed out using the streambufpointer produced with rdbuf( ). Finally, the overloaded version of seekg( )is used with the streampos sp2to move to the previous position, and the last portion of the file is printed out.

posted @ 2014-09-24 11:40  小菜庞  阅读(267)  评论(0编辑  收藏  举报