C++ Primer 5th notes (chap 17 standard library special facilities) stream random access

1. Stream types usually support random access to data in streams.

You can relocate the stream to skip some data, reading the last row first, then the first row, and so on.

1.1 the standard database provides a pair of functions to seek to a given location in the stream and tell us the current location

  • Seek function: relocate the tag seek to a given location;
  • Tell function: tell the current position of the tag.

The standard library actually defines two pairs of seek and tell functions:

  • The g version indicates that we are "getting" (reading) data
  • The p version indicates that we are "placing" (writing) data

Applies only to fstream and s sstream.

operationexplain
tellg()Tellp returns the current position of a tag in an input stream (tellg) or output stream (tellp).
seekg(pos)seekp(pos) relocates the tag to a given absolute address in an input or output stream. pos is usually a value returned by the current teelg or tellp.
seekp(off, from) seekg(off, from)In an input stream or output stream, position the tag to off characters before or after from. From can be one of the following values: beg, offset relative to the start of the stream; cur, offset relative to the current position of the stream; end, offset relative to the end of the stream.

1.2 only one mark

The "put" and "get" versions of the standard seek and tell functions can be misleading.
Even though the standard library is differentiated, it maintains only a single tag in a stream - there are no separate read and write tags

Since there is only a single tag, as long as we switch between read and write operations, we must perform the seek operation to reposition the tag

1.3 relocation marks

The seek function has two versions: one is moved to the "absolute" address in the file; the other is moved to the specified offset at a given location:

//Move the marker to a fixed position
seekg (new_position); / / Moves the read mark to the specified location pos_type Location of type
seekp (new_position); / / Moves the write mark to the specified location pos_type Location of type
/ / Moves to the offset position specified before or after a given start point
seekg (offset, from); / / Move read mark to space from Offset is offset Location of
seekp (offset, from); / / Move write mark to space from Offset is of fset Location of
  • pos_type indicates a file location
  • off_type indicates an offset from the current position.
    An off_type value can be positive or negative, that is, it can move forward or backward in the file.

1.4 access marking

The functions tellg and tellp return a pos_type indicating the current location of the stream. The tell function is usually used to remember a location so that it can be located later

ostringstream::writeStr;// Output stringstream
ostringstream::pos_type mark = writeStr.tellp ( );
// ...
if (cancelEntry)
/ / Go back to the position you just remembered
writeStr.seekp (mark);

/ / Open the file in read-write mode and navigate to the end of the file
fstream inOut("test_2.txt", fstream::ate|fstream::in|fstream::out);
    if(!inOut){
        return EXIT_FAILURE;
    }

/ / inOut with ate Mode is turned on, so it is defined from the beginning to the end of its file
    auto end_mark = inOut.tellg();//Remember the end of the original file
    inOut.seekg(0,fstream::beg);/ / Relocate to the beginning of the file
    size_t cnt = 0;/ / Byte accumulator
    string line;/ / Save each line in the input
    while(inOut&&inOut.tellg() != end_mark&&getline(inOut,line)){/ / Conditions to continue reading: no error has been encountered and the original data is still being read/ / One line of input can also be obtained
        cnt += line.size()+1;/ / Plus 1 indicates a line break
        cout << line.size() << endl;
        auto mark = inOut.tellg();/ / Remember where to read
        inOut.seekp(0,fstream::end);II Move write mark to end of file
        inOut << cnt;/ / Output cumulative length
/ / If it is not the last line, print a separator
        if(mark != end_mark)
        inOut << " ";
        inOut.seekg(mark);/ / Restore read location
    }
    inOut.seekp(0,fstream::end);

Original file:

abed
efg
hi
j

Modified file:

abed
efg
hi
j5
9 12 14

Tags: C++

Posted on Tue, 16 Nov 2021 19:28:56 -0500 by RClapham