简体   繁体   English

tellg()和tellp()在C ++中如何工作?

[英]How do tellg() & tellp() work in C++?

Why do tellg() and tellp() always come together? 为什么tellg()tellg() tellp()总是在一起? And how can we separate them from each other? 我们如何将它们彼此分开?

They don't. 他们没有。

One indicates where the "get" cursor is in an input stream; 一个指示“ get”光标在输入流中的位置。 the other indicates where the "put" cursor is in an output stream. 另一个指示“放置”光标在输出流中的位置。

They are similar but not intrinsically linked, so the premise of your question is fundamentally flawed. 它们是相似的,但没有内在联系,因此,您提出问题的前提从根本上来说是有缺陷的。

For starters, they're members of different classes: tellg is a member of std::istream , and tellp is a member of std::ostream . 对于初学者来说,他们是不同类的成员: tellg是其成员std::istream ,并且tellp是成员std::ostream The reason they have different names is because there are classes (eg std::iostream ) which derive from both std::istream and std::ostream , and the functions may report different values: an actual std::streambuf type may maintain the read and write positions in sync (as does std::filebuf ) or keep them separate (as does std::stringbuf ). 它们之所以具有不同的名称,是因为存在从std::istreamstd::ostream派生的类(例如std::iostream ),并且这些函数可能报告不同的值:实际的std::streambuf类型可以维护同步读取和写入位置(如std::filebuf )或将它们分开(如std::stringbuf )。

One is for an input stream, the other for an output stream. 一个用于输入流,另一个用于输出流。

http://en.cppreference.com/w/cpp/io/basic_istream/tellg input stream position http://en.cppreference.com/w/cpp/io/basic_istream/tellg输入流位置

http://en.cppreference.com/w/cpp/io/basic_ostream/tellp output stream position http://en.cppreference.com/w/cpp/io/basic_ostream/tellp输出流位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM