Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ tellg() return type

Tags:

c++

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. However, based on the C++ documentation, it isn't clear to me if:

  1. What is the return type of tellg()
  2. How can I compare tellg() with the unsigned long long int?
  3. 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?

Any answers or suggestions would be greatly appreciated.

like image 214
slaw Avatar asked Apr 10 '14 18:04

slaw


People also ask

What does Tellg return?

The tellg() function is used with input streams, and returns the current “get” position of the pointer in the stream. It has no parameters and returns a value of the member type pos_type, which is an integer data type representing the current position of the get stream pointer.

Why does Tellg return?

Because file doesn't exist. So you can't open. That's why returned -1.


1 Answers

Q What is the return type of tellg()?

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

Q How can I compare tellg() with the unsigned long long int?

A The return value of tellg() is an integral type. So you can use the usual operators to compare two ints. 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.

Check out 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?

A The standard does not make any claims to support it or refute it. It might be true in one platform while false in another.

Additional Info

Comparing streampos, examples of supported and unsupported compare 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
{
}
like image 98
R Sahu Avatar answered Sep 21 '22 08:09

R Sahu