📜  C++中的tellg()函数与示例

📅  最后修改于: 2021-05-30 10:19:11             🧑  作者: Mango

tellg()函数与输入流一起使用,并返回指针在流中的当前“获取”位置。它没有参数,并返回成员类型pos_type的值,该值是表示get流指针的当前位置的整数数据类型。

句法:-

pos_type tellg(); 

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

例子1

// C++ program to demonstrate 
// example of tellg() function.
#include 
using namespace std;
  
int main()
{
    string str = "geeksforgeeks";
    istringstream in(str);
  
    string word;
    in >> word;
    cout << "After reading the word \"" << word
        << "\" tellg() returns " << in.tellg() << '\n';
}
输出:
After reading the word "geeksforgeeks" tellg() returns -1

范例2:

// C++ program to demonstrate 
// example of tellg() function.
#include 
using namespace std;
  
int main()
{
    string str = "Hello, GFG";
    istringstream in(str);
  
    string word;    
    in >> word;
      
    cout << "After reading the word \"" << word
        << "\" tellg() returns " << in.tellg() << '\n';
}
输出:
After reading the word "Hello," tellg() returns 6

特性:-
tellg()不会报告文件的大小,也不会报告从开头的偏移量(以字节为单位)。它报告一个令牌值,该令牌值以后可用于查找同一位置,仅此而已。 (甚至不保证您可以将类型转换为整数类型)

想要从精选的最佳视频中学习和练习问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”