Search code examples
c++positionbinaryfilesfstream

are "seekp" & "seekg" interchangeable?


Well I just noticed that by changing the position -in microsoft visual studio- through "seekp" I implicitelly also change the read-position, when handling files.

I am wondering however if this is "portable" behaviour? Can I expect the position of reading & writing to be always the same? And consequently: will tellp & tellg always return the same value?


Solution

  • For file positions they are the same. In other words there is only one pointer maintained.

    From 27.9.1.1p3:

    A joint file position is maintained for both the input sequence and the output sequence.

    So, seekg and seekp are interchangeable for file streams. However, this is not true for other types of streams, as they may hold separate pointers for the put and get positions.