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:
Any answers or suggestions would be greatly appreciated.
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.
Because file doesn't exist. So you can't open. That's why returned -1.
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 int
s. 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.
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
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With