On C++ primer chapter 17; I/O streams; it is said that an object of type std::iostream or a derived type like std::fstream, std::stringstream support both reading and writing. Also these types have only one Marker and a single Buffer:
The
fstreamandstringstreamtypes can read and write the same stream. In these types there is a single buffer that holds data to be read and written and a single marker denoting the current position in the buffer. The library maps both thegandppositions to this single marker.
So to understand more I've done this program:
int main(){
std::fstream fio("data.txt");
std::cout << fio.tellg() << '\t' << fio.tellp() << '\n';
fio.seekg(0, std::ios::end);
std::cout << fio.tellg() << '\t' << fio.tellp() << '\n';
std::stringstream ss("abcd\nefg\nhi\nj");
std::cout << ss.tellg() << '\t' << ss.tellp() << '\n';
ss.seekg(0, std::ios::end);
std::cout << ss.tellg() << '\t' << ss.tellp() << '\n';
}
The output:
0 0
14 14
0 0
13 0
As you can see when using
std::fstreamif I move the read marker usingseekg()then thetellgandtellpreturn the same value. Which is so logical.However when moving the read marker for
std::stringstream,tellgandtellpreturn different value so the write marker hasn't been moved?Does this mean
std::stringstreamhas two buffers and two markers? Thanks