seekg and tellg with random file i/o

I know the following:

1) seekg method actually moves the file pointer. The g stands for “get” so it moves the get pointer. It takes two arguments. The first parameter is an offset that determines how many bytes to move the file pointer. The second parameter is an ios flag that specifies what the offset parameter should be offset from.

2) tellg() get the position of the get pointer and tellp() gets the position of the put pointer. So tellg() is the place where you read in the file.

Given that knowledge, I have the following line of code:

iofile.seekg(iofile.tellg(), ios::beg); // seek to current file position

This understand what is going on. The first thing the compiler does it is call iofile.tellg(). This returns the number of bytes from the beginning of the file that the get pointer is at. In other words, it tells you how many characters into the file the pointer is currently at. The ios::beg is a flag passed to seekg that tells seekg where to move the pointer - in this case we move it to the beginning of the file. Hence, what this expression does is moves the pointer to the beginning of the file and then offsets the pointer n bytes where n is the number of bytes that tellg returns. In other words, it seems to be moving the pointer in the same exact position it started at! So what is the point of this expression? Am I missing something?
I'm pretty sure you are right; that snippet of code doesn't do anything useful as far as I can tell.
tellg() get the position of the get pointer and tellp() gets the position of the put pointer

Small caveat: files have only one pointer, so tellp and tellg on a file stream will always give you the same value (they are different on other streams, e.g. stringstream)

And yes, seekg to tellg does nothing directly, although there are side-effects this can be used for: seekg will undo any putbacks/ungets, for example
Topic archived. No new replies allowed.