79

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?

Tu.Ma.
  • 1,325
  • 10
  • 27
Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
  • 5
    It it was called only plain `seek` (for example), then you can't have separate pointers for the input and output parts of a combined input-*and*-output stream. – Some programmer dude Dec 04 '18 at 08:30
  • 1
    By the way: The answers explain the p too. It may be more useful to edit the question for the p. – user202729 Dec 04 '18 at 10:53

2 Answers2

90

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.

Alex Taylor
  • 8,343
  • 4
  • 25
  • 40
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
55

C++ offers two pointers while navigating the file: the get pointer and the put pointer. The first one is used for read operations, the second one for write operations.

  • seekg() is used to move the get pointer to a desired location with respect to a reference point.

  • tellg() is used to know where the get pointer is in a file.

  • seekp() is used to move the put pointer to a desired location with respect to a reference point.

  • tellp() is used to know where the put pointer is in a file.

Main source: Quora, answer by Gunjan B. Yadav on Dec 1, 2017.

Community
  • 1
  • 1
Tu.Ma.
  • 1,325
  • 10
  • 27
  • 19
    Why do C++ standards people name APIs as if keyboards have per-keystroke costs. – Alexander Dec 05 '18 at 00:33
  • 14
    @Alexander : to avoid people getting paid by keystroke, obviously. Clever and business-oriented programmers naturally overcome this problem by inventing their own types and wrappers for the standard library, which of course always works out perfectly and has no disadvantages whatsoever. – Daniel Kamil Kozar Dec 05 '18 at 00:35
  • 1
    @Alexander https://en.cppreference.com/w/cpp/chrono/high_resolution_clock – Ajay Dec 05 '18 at 07:09
  • 1
    @Ajay I like it! – Alexander Dec 05 '18 at 18:41