tellg(), seekg() and seekp() functions are used to set/get the position of get and put pointers in a file while reading and writing.
Syntaxes:
// tellg()
streampos tellg();
// seekg()
istream& seekg (streampos pos);
istream& seekg (streamoff off, ios_base::seekdir way);
// seekp()
stream& seekp (streampos pos);
ostream& seekp (streamoff off, ios_base::seekdir way);
Here,
- pos – represents the new absolute position within the stream (from the beginning).
- off – represents the offset to seek.
- pos – represents the following constants,
- ios_base::beg / ios::beg – beginning of the stream.
- ios_base::cur / ios::cur – current position in the stream.
- ios_base::end / ios::end – end the stream.
C++ program to demonstrate the example of tellg(), seekg() and seekp()
In the below program, we are using a file 'my.txt', the file contains the following text,
File: my.txt
Program:
Output
After the program execution the file content is,
need an explanation for this answer? contact us directly to get an explanation for this answer