命名空间
变体
操作

std::basic_istream<CharT,Traits>::tellg

来自 cppreference.cn
< cpp‎ | io‎ | basic istream
 
 
 
 
pos_type tellg();

返回当前关联的 streambuf 对象的输入位置指示器。

行为如同 UnformattedInputFunction,除了 gcount() 不受影响。在构造并检查哨兵对象后,如果 fail() == true,则返回 pos_type(-1)。否则,返回 rdbuf()->pubseekoff(0, std::ios_base::cur, std::ios_base::in)

目录

[edit] 参数

(无)

[edit] 返回值

成功时,返回 get 指针的当前位置;失败时,返回 pos_type(-1)

[edit] 异常

如果发生错误(错误状态标志不是 goodbit)且 exceptions() 设置为针对该状态抛出异常,则抛出 failure

如果内部操作抛出异常,则会捕获该异常并设置 badbit。如果为 badbit 设置了 exceptions(),则会重新抛出该异常。

[edit] 示例

#include <iostream>
#include <sstream>
#include <string>
 
int main()
{
    std::string str = "Hello, world";
    std::istringstream in(str);
    std::string word;
    in >> word;
    std::cout << "After reading the word \"" << word
              << "\" tellg() returns " << in.tellg() << '\n';
}

输出

After reading the word "Hello," tellg() returns 6

[edit] 参见

[虚拟]
使用相对寻址重新定位文件位置
(std::basic_filebuf<CharT,Traits> 的虚拟受保护成员函数) [编辑]
[虚拟]
使用相对寻址重新定位输入序列、输出序列或两者中的下一个指针
(std::basic_stringbuf<CharT,Traits,Allocator> 的虚拟受保护成员函数) [编辑]
[虚拟]
使用相对寻址重新定位输入序列、输出序列或两者中的下一个指针
(std::strstreambuf 的虚拟受保护成员函数) [编辑]
设置输入位置指示器
(公共成员函数) [编辑]
返回输出位置指示器
(std::basic_ostream<CharT,Traits> 的公共成员函数) [编辑]
设置输出位置指示器
(std::basic_ostream<CharT,Traits> 的公共成员函数) [编辑]