C++20 sections · 1024 units
Open in Course

tellg and tellp

Querying current position

tellg() returns the current read position in bytes, tellp() returns the write position. Use them to save positions: auto pos = file.tellg(); later file.seekg(pos); restores the exact find.

Get file size by seeking to end: file.seekg(0, ios::end); auto size = file.tellg(); file.seekg(0, ios::beg);. The position at end equals file size in bytes. These return streampos type (alias for long long on most systems).

You can compare positions or calculate distances: auto bytesRead = file.tellg() - startPos;. Positions are byte offsets from file start.