[英]C++ tellg() return type
I have a large binary file that I am reading and I would like to compare the current position with an unsigned long long int. 我有一个大的二进制文件,我正在阅读,我想比较当前位置与unsigned long long int。 However, based on the C++ documentation, it isn't clear to me if:
但是,基于C ++文档,我不清楚是否:
Any answers or suggestions would be greatly appreciated. 任何答案或建议将不胜感激。
Q What is the return type of tellg()? 问 :tellg()的返回类型是什么?
A The return type of istream::tellg()
is streampos
. A
istream::tellg()
的返回类型是streampos
。 Check out std::istream::tellg . 看看std :: istream :: tellg 。
Q How can I compare tellg() with the unsigned long long int? 问:如何将tellg()与unsigned long long int进行比较?
A The return value of tellg()
is an integral type. A
tellg()
的返回值是一个整数类型。 So you can use the usual operators to compare two int
s. 所以你可以使用通常的运算符来比较两个
int
。 However, you are not supposed to do that to draw any conclusions from them. 但是,您不应该这样做以从中得出任何结论。 The only operations that the standard claims to support are:
标准声称支持的唯一操作是:
Two objects of this type can be compared with operators == and !=.
可以将这种类型的两个对象与运算符==和!=进行比较。 They can also be subtracted, which yields a value of type streamoff.
它们也可以减去,产生类型streamoff的值。
Check out std::streampos . 看看std :: streampos 。
Q Is it possible that the return type of tellg() has a maximum value (from numeric_limits) that is smaller than an unsigned long long int? 问 :tellg()的返回类型是否有可能小于unsigned long long int的最大值(来自numeric_limits)?
A The standard does not make any claims to support it or refute it. A该标准不作任何声明支持或反驳它。 It might be true in one platform while false in another.
在一个平台上可能是真的,而另一个平台则是假的。
Comparing streampos
, examples of supported and unsupported compare operations 比较
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
{
}
R Sahu did a good job on answering the questions, however I was getting overflows when storing the result of .tellg()
in an int
and did some extra investigation. R Sahu在回答问题方面做得很好,但是当将
.tellg()
的结果存储在int
时我得到了溢出,并做了一些额外的调查。
std::streamoff
(read as "stream offset") as a integer type to store the result of tellg()
. std::streamoff
(读作“stream offset”)作为整数类型来存储tellg()
的结果。 From browsing std::basic_ifstream : 从浏览std :: basic_ifstream :
pos_type tellg();
where the pos_type
is defined by the Traits
template arguments, which in turn is implementation defined. 其中
pos_type
由Traits
模板参数定义,而pos_type
模板参数又是实现定义的。 For std::ifstream the Traits is std::char_traits of type char, which leads to std::fpos . 对于std :: ifstream,Traits是char类型的std :: char_traits ,它导致std :: fpos 。 Here we see:
我们在这里看到:
Each object of type fpos holds the byte position in the stream (typically as a private member of type std::streamoff ) and the current shift state, a value of type State (typically std::mbstate_t).
fpos类型的每个对象都保存流中的字节位置(通常作为std :: streamoff类型的私有成员 )和当前的shift状态,State类型的值(通常是std :: mbstate_t)。
(bolding done by me) (由我完成的粗体)
Hence, to avoid overflows it should be safe to cast the result of tellg()
to whatever the type of std::streamoff
is. 因此,为了避免溢出,将
tellg()
的结果tellg()
为std::streamoff
tellg()
的类型应该是安全的。 Further, checking std::streamoff it says 此外,检查std :: streamoff它说
The type std::streamoff is a signed integral type of sufficient size to represent the maximum possible file size supported by the operating system.
类型std :: streamoff是一个足够大小的有符号整数类型,表示操作系统支持的最大可能文件大小。 Typically, this is a typedef to long long .
通常,这是一个很长的typedef。
As the exact type is defined by your system, it's probably a good idea to run a quick test. 由于确切的类型是由您的系统定义的,因此运行快速测试可能是个好主意。 Here is the result on my machine:
这是我机器上的结果:
std::cout << "long long:" std::is_same<std::ifstream::off_type, long long>::value << std::endl;
std::cout << "long:" std::is_same<std::ifstream::off_type, long>::value << std::endl;
// Outputs
// long long:0
// long:1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.