g 在 gcount、tellg 和 seekg 中代表什么?

What does the g stand for in gcount, tellg and seekg?

gstd::iostreamgcounttellgseekg成员中代表什么?以及 pcounttellpseekp?

中的 p

为什么他们不叫 counttellseek

C++ 在浏览文件时提供了两个指针:get 指针和 put 指针。第一个用于读操作,第二个用于写操作。

  • 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.

主要来源:QuoraGunjan B. Yadav 于 2017 年 12 月 1 日回答。

在同时支持读和写的流中,实际上有两个位置,一个用于读取(即 "get" 表示为 "g"),一个用于写入(即 "put" 表示为"p").

这就是为什么你有一个 seekp(继承自 basic_ostream)和一个 seekg(继承自 basic_istream)。

旁注:与 C++ 相比,C 语言只有一个这样的函数 fseek 用于两个指针;从读取切换到写入时需要重新定位指针,反之亦然(参见,例如, 答案)。为了避免这种情况,C++ 分别为读和写提供了单独的函数。