Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 680
paul23 Avatar asked Jan 15 '13 00:01

paul23


People also ask

What is Seekp function?

The seekp(pos) method of ostream in C++ is used to set the position of the pointer in the output sequence with the specified position. This method takes the new position to be set and returns this ostream instance with the position set to the specified new position.

What is Seekp used for in C++?

Whereasseekp() function is used to move/sets the put pointer at the particular/desired location to putting the data into a file (External file). And If seekp() function is used to move the put pointer at the particular/desired location.

What is the difference between seekg and Seekp?

Use seekp() to position the file position before writing. Use seekg() to position the file position before reading. If you use one, don't for get to use the other.

What is seekg () Seekp () function in C++?

C++ tellg(), seekg() and seekp() Example tellg(), seekg() and seekp() functions are used to set/get the position of get and put pointers in a file while reading and writing.

What is the difference between seekg and seekp?

In other words there is only one pointer maintained. 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.

What does seekp () do in fstream?

We have a function called seekp which gets imported along with the other basic file handling operation in “fstream”. So what does seekp () do? The seekp () function moves the pointer to the desired location. When we create a text file or open a text file, our pointer is set to 0.

What is the use of gettellg () and seekp () functions?

tellg (), seekg () and seekp () functions are used to set/get the position of get and put pointers in a file while reading and writing. pos – represents the new absolute position within the stream (from the beginning). off – represents the offset to seek. ios_base::beg / ios::beg – beginning of the stream.


2 Answers

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.

like image 155
Jesse Good Avatar answered Oct 19 '22 17:10

Jesse Good


Update: So from all the comments and everything, it seems that for fstream, seekp and seekg use the same pointer. But for stringstream and probably other non-file based streams, they are separate.


Original Post:

Doesn't work for me on linux with g++ 4.7.2. They seem to be independent:

#include <sstream> #include <iostream>  int main(int, char**) {     std::stringstream s("0123456789");     std::cout << "put pointer: " << s.tellp() << std::endl;     std::cout << "get pointer: " << s.tellg() << std::endl;     std::cout << std::endl;     s.seekp(2);     std::cout << "put pointer: " << s.tellp() << std::endl;     std::cout << "get pointer: " << s.tellg() << std::endl;     std::cout << std::endl;     s.seekg(4);     std::cout << "put pointer: " << s.tellp() << std::endl;     std::cout << "get pointer: " << s.tellg() << std::endl;     std::cout << std::endl; } 

Output:

put pointer: 0 get pointer: 0  put pointer: 2 get pointer: 0  put pointer: 2 get pointer: 4 

Also the behaviour you describe sounds like it doesn't comply with the quotes here:

Sets the position of the get pointer. The get pointer determines the next location to be read in the source associated to the stream.

and here:

Sets the position of the put pointer. The put pointer determines the location in the output sequence where the next output operation is going to take place.

like image 31
matiu Avatar answered Oct 19 '22 16:10

matiu