What does the g stand for in std::iostream
's gcount
, tellg
and seekg
members? And the p in pcount
, tellp
and seekp
?
Why aren't they called just count
, tell
and seek
?
The tellg() function is used with input streams, and returns the current “get” position of the pointer in the stream. It has no parameters and returns a value of the member type pos_type, which is an integer data type representing the current position of the get stream pointer.
tellp() gives the position of the put pointer. tellg() gives the position of the get pointer.
seekg() and seekp() both are functions of File Handling in C++ and they are very important and useful feature of File Handling in C++. In File Handling of C++, we have two pointers one is get pointer and second is put pointer.
seekg() is a function in the iostream library that allows us to seek an arbitrary position in a file. It is mainly used to set the position of the next character to be extracted from the input stream from a given file in C++ file handling.
In streams supporting both read and write, you actually have two positions, one for read (i.e. "get" denoted by "g") and one for write (i.e. "put" denoted by a "p").
And that's why you have a seekp
(inherited from basic_ostream
), and a seekg
(inherited from basic_istream
).
Side note: The language C has - in contrast to C++ - only one such function fseek
for both pointers; There it is necessary to re-position the pointer when switching from read to write and vice versa (cf., for example, this answer). To avoid this, C++ offers separate functions for read and write, respectively.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With