gpt4 book ai didi

C++ tellg() 返回类型

转载 作者:可可西里 更新时间:2023-11-01 17:54:31 29 4
gpt4 key购买 nike

我有一个正在读取的大型二进制文件,我想将当前位置与一个 unsigned long long int 进行比较。但是,根据 C++ 文档,我不清楚是否:

  1. tellg() 的返回类型是什么
  2. 如何比较 tellg() 与 unsigned long long int?
  3. 是否有可能 tellg() 的返回类型具有小于 unsigned long long int 的最大值(来自 numeric_limits)?

如有任何答案或建议,我们将不胜感激。

最佳答案

tellg()的返回类型是什么?

A istream::tellg() 的返回类型是streampos。查看std::istream::tellg .

如何比较 tellg() 和 unsigned long long int?

A tellg()的返回值是整型。因此,您可以使用常用的运算符来比较两个 int。但是,您不应该这样做以从中得出任何结论。该标准声称支持的唯一操作是:

Two objects of this type can be compared with operators == and !=. They can also be subtracted, which yields a value of type streamoff.

查看 std::streampos .

是否有可能 tellg() 的返回类型具有小于 unsigned long long int 的最大值(来自 numeric_limits)?

A 该标准没有做出任何声明来支持它或反驳它。它在一个平台上可能是真的,而在另一个平台上可能是假的。

附加信息

比较streampos,支持和不支持的比较操作示例

ifstream if(myinputfile);
// Do stuff.
streampos pos1 = if.tellg();
// Do more stuff
streampos pos2 = if.tellg();

if ( pos1 == pos2 ) // Supported
{
// Do some more stuff.
}

if ( pos1 != pos2 ) // Supported
{
// Do some more stuff.
}

if ( pos1 != pos2 ) // Supported
{
// Do some more stuff.
}

if ( pos1 == 0 ) // supported
{
// Do some more stuff.
}

if ( pos1 != 0) // supported
{
// Do some more stuff.
}

if ( pos1 <= pos2 ) // NOT supported
{
// Do some more stuff.
}


int k = 1200;
if ( k == pos1 ) // NOT supported
{
}

关于C++ tellg() 返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22996683/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com