std::basic_istream<CharT,Traits>::tellg
来自cppreference.com
| |
||
返回当前关联的 streambuf 对象的输入位置指示器。
表现为无格式输入函数 (UnformattedInputFunction) ,但不影响 gcount()。构造并检查 sentry 对象后,若 fail() == true,则返回 pos_type(-1)。否则,返回 rdbuf()->pubseekoff(0, std::ios_base::cur, std::ios_base::in)。
参数
(无)
返回值
成功时为获取指针的当前位置,失败时为 pos_type(-1)。
异常
在出现错误(错误状态标志不是 goodbit)并且 exceptions() 已设置为对该状态抛出时,会抛出 failure。
如果内部操作抛出了异常,那么捕获它并设置 badbit。如果 exceptions() 设置了 badbit,那么就会重抛该异常。
示例
运行此代码
#include <iostream>
#include <sstream>
#include <string>
int main()
{
std::string str = "Hello, world";
std::istringstream in(str);
std::string word;
in >> word;
std::cout << "读取 \"" << word
<< "\" 后 tellg() 返回 " << in.tellg() << '\n';
}
输出:
读取 "Hello," 后 tellg() 返回 6
参阅
[虚] |
用相对寻址重寻位文件位置 ( std::basic_filebuf<CharT,Traits> 的虚受保护成员函数)
|
[虚] |
用相对寻址,重定位输入序列、输出序列或两者中的下一位置指针 ( std::basic_stringbuf<CharT,Traits,Allocator> 的虚受保护成员函数)
|
[虚] |
用相对寻址重寻位输入序列、输出序列或两者中的下一位置指针 ( std::strstreambuf 的虚受保护成员函数)
|
| 设置输入位置指示器 (公开成员函数) | |
| 返回输出位置指示器 ( std::basic_ostream<CharT,Traits> 的公开成员函数)
| |
| 设置输出位置指示器 ( std::basic_ostream<CharT,Traits> 的公开成员函数)
|