C ++ tellg () return type

I have a large binary that I am reading and I would like to compare the current position with unsigned long long int. However, based on the C ++ documentation, it is not clear to me if:

  • What is the return type tellg ()
  • How to compare tellg () with unsigned long long int?
  • Is it possible that the return type tellg () has a maximum value (from numeric_limits) that is less than unsigned long long int?

Any answers or suggestions are welcome.

+8
c ++
source share
2 answers

Q What is the return type of tellg ()?

A The return type istream::tellg() is streampos . Check out std :: istream :: tellg .

Q How to compare tellg () with unsigned long long int?

A Return value tellg() is an integral type. So you can use regular operators to compare two int s. However, you should not do this in order 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 the operators == and! =. They can also be subtracted, giving a streamoff value.

Check std :: streampos .

Q Is it possible that the return type tellg () has a maximum value (from numeric_limits) that is less than unsigned long long int?

A The standard does not purport to support or disprove it. This may be true on one platform, while false on another.

Additional Information

Comparison of streampos , examples of supported and unsupported comparison operations

 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 { } 
+6
source share

R Sahu did a good job of answering the questions, however I got overflows while saving the result of .tellg() in int and did some more research.

TL; DR: use std::streamoff (read as "stream offset") as an integer type to store the tellg() result.

From the std :: basic_ifstream view :

 pos_type tellg(); 

where pos_type determined by the arguments of the Traits template, which, in turn, are determined by the implementation. For std :: ifstream Traits is of type std :: char_traits of type char, which leads to std :: fpos . Here we see:

Each object of type fpos contains the byte position in the stream (usually as a private member of type std :: streamoff ) and the current state of the shift, a value of type State (usually std :: mbstate_t).

(courage is done by me)

Therefore, to avoid overflow, it should be safe to cast the result of tellg() to some type of std::streamoff . Next, checking std :: streamoff says

The type std :: streamoff is an integer type with a sign of sufficient size to represent the maximum possible file size supported by the operating system. This is usually a typedef for long long .


Since the exact type is determined by your system, it might be worth running 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 
0
source share

All Articles