std::streampos and INT_MAX - incompatible?

Discussion in 'C++' started by Al-Burak, Dec 22, 2005.

  1. Al-Burak

    Al-Burak Guest

    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.
    Al-Burak, Dec 22, 2005
    #1
    1. Advertising

  2. Al-Burak

    Guest

    Al-Burak wrote:
    > 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
    , Dec 22, 2005
    #2
    1. Advertising

  3. Al-Burak

    mlimber Guest

    Al-Burak wrote:
    > 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
    mlimber, Dec 22, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Huang
    Replies:
    1
    Views:
    362
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Mar 27, 2005
  2. Al-Burak
    Replies:
    1
    Views:
    618
    Jim Langston
    Dec 26, 2005
  3. Jacek Dziedzic
    Replies:
    1
    Views:
    753
    Jacek Dziedzic
    Feb 6, 2007
  4. Wolfgang Thomsen

    Saving and restoring streampos's

    Wolfgang Thomsen, Apr 25, 2007, in forum: C++
    Replies:
    1
    Views:
    378
    James Kanze
    Apr 26, 2007
  5. mathieu
    Replies:
    1
    Views:
    916
    Victor Bazarov
    Jul 18, 2011
Loading...

Share This Page