C++中的TelGe()函数及其实例

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

null

语法:-

pos_type tellg(); 

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

例1

// C++ program to demonstrate
// example of tellg() function.
#include <bits/stdc++.h>
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() << '' ;
}


输出:

After reading the word "geeksforgeeks" tellg() returns -1

例2:

// C++ program to demonstrate
// example of tellg() function.
#include <bits/stdc++.h>
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() << '' ;
}


输出:

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

属性:- tellg()不报告文件的大小,也不报告从开始到结束的偏移量(字节)。它报告了一个令牌值,以后可以用来寻找同一个地方,仅此而已。(甚至不能保证可以将类型转换为整型)

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享