std::streampos and INT_MAX - incompatible?

A

Al-Burak

I have a program that retrieves the cursor position at the point where
the data began to be stored, this value is assigned to a
'std::streampos' data type called 'pos'. Later on in the program, 'pos'
is put in another file, but as an 'int' data type, as suggested by the
documentation. Having said that, here is my question: If the value of
'pos' exceeds INT_MAX, I will lose the ability to store that data in an
integral value; run time error kind of thing (at best!), I suspect.
This is most likely to happen since the file size will most definitely
grow to a size bigger than INT_MAX.

Can any pitch-in on a work around?


Thanks in advance.
 
M

Michiel.Salters

Al-Burak said:
I have a program that retrieves the cursor position at the point where
the data began to be stored, this value is assigned to a
'std::streampos' data type called 'pos'. Later on in the program, 'pos'
is put in another file, but as an 'int' data type, as suggested by the
documentation. Having said that, here is my question: If the value of
'pos' exceeds INT_MAX, I will lose the ability to store that data in an
integral value; run time error kind of thing (at best!), I suspect.

On the risk of sounding obvious: why not store std::streampos values
in std::streampos objects? That won't overflow.

HTH,
Michiel Salters
 
M

mlimber

Al-Burak said:
I have a program that retrieves the cursor position at the point where
the data began to be stored, this value is assigned to a
'std::streampos' data type called 'pos'. Later on in the program, 'pos'
is put in another file, but as an 'int' data type, as suggested by the
documentation. Having said that, here is my question: If the value of
'pos' exceeds INT_MAX, I will lose the ability to store that data in an
integral value; run time error kind of thing (at best!), I suspect.
This is most likely to happen since the file size will most definitely
grow to a size bigger than INT_MAX.

Can any pitch-in on a work around?


Thanks in advance.

My implementation bears this comment in postypes.h:

// The types streamoff, streampos and wstreampos and the class
// template fpos<> are described in clauses 21.1.2, 21.1.3, 27.1.2,
// 27.2, 27.4.1, 27.4.3 and D.6. Despite all this verbage, the
// behaviour of these types is mostly implementation defined or
// unspecified. The behaviour in this implementation is as noted
// below.

So, you either need to convert the streampos to an integral type that
is large enough for your needs, or you need to supply an operator to
stream your implementation's version of streampos. In either case, it
will likely be implementation-dependent.

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top