0
Seekg() and tellg()
I am learnig c++ file handling and i stumbled upon these words. Can anyone help me to understand these two functions?
1 ответ
+ 6
In all files, there is a cursor, that can be moved forward or backward, to set the reading position. Similarly, there exists a similar writing cursor.
The seekg() method, is used to set the position of the reading cursor, so that one can read data from some other location in the file.
Eg - file.seekg(2); //Sets cursor at 2 steps forward, Now reading starts from 3rd character.
There is also an offset setting, to set the relative position of the cursor from the standard locations, beg, end and cur.
Eg - file.seekg(-2,file.cur); //Move the cursor back 2 steps from the current position.
The tellg() method, is used to get the current position of the reading cursor. You may save this position in an int or a streampos variable.
The methods above are accessible only when you have opened the file in read mode (ios::in).
Similar functions seekp and tellp exist for the writing cursor, but require the ios::out mode.
For more info, read this:
www.cplusplus.com/doc/tutorial/files/