Re: [C++] Rewinding an isteam and GNU G++

From: B. v Ingen Schenau (bart_at_ingen.ddns.info)
Date: 11/01/04


Date: Mon, 01 Nov 2004 19:08:35 +0100

Thomas Matthews wrote:

> Hi,
>
> I've searched the FAQ and no tips on this one.
> Searching the newsgroups shows that the proper
> method for rewinding an istream is:
> istream inp;
> // read past EOF.
> inp.clear(); // Clear the stream state
> inp.seekg(0);

I believe the correct method for rewinding is:

istream inp;
istream::pos_type beginning = inp.tellg();
// read past EOF
inp.clear();
inp.seekg(beginning);

It is possible that most implementations support the call inp.seekg(0),
but formally are the effects undefined.
istream::seekg(pos) calls, indirectly, basic_filebuff<>::seekpos(pos).
The standard states about basic_filebuf<>::seekpos():
"If sp is an invalid stream position, or if the function positions
neither sequence, the positioning operation fails. If sp has not been
obtained by a previous successful call to one of the positioning
functions (seekoff or seekpos) on the same file the effects are
undefined."

>
> I've used the above technique with Borland
> C++ Builder and it works. However, it doesn't
> work with GNU g++ 3.3.1 (cygwin special).
> Here is a small demo program (annotated):
>
<snip - test program>
> So, what is the standard method for rewinding?
> Which compiler is correct?

I think both are correct. :-)

> Can somebody test with Microsoft?
>

Bart v Ingen Schenau

-- 
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/


Relevant Pages

  • Re: [C++] Rewinding an isteam and GNU G++
    ... > I've searched the FAQ and no tips on this one. ... > Searching the newsgroups shows that the proper ... I believe the correct method for rewinding is: ... neither sequence, the positioning operation fails. ...
    (comp.lang.cpp)
  • [C++] Rewinding an isteam and GNU G++
    ... I've searched the FAQ and no tips on this one. ... Searching the newsgroups shows that the proper ... what is the standard method for rewinding? ...
    (alt.comp.lang.learn.c-cpp)
  • [C++] Rewinding an isteam and GNU G++
    ... I've searched the FAQ and no tips on this one. ... Searching the newsgroups shows that the proper ... what is the standard method for rewinding? ...
    (comp.lang.cpp)
  • Re: [C++] Rewinding an isteam and GNU G++
    ... > I've searched the FAQ and no tips on this one. ... > Searching the newsgroups shows that the proper ... what is the standard method for rewinding? ...
    (alt.comp.lang.learn.c-cpp)
  • Re: [C++] Rewinding an isteam and GNU G++
    ... > I've searched the FAQ and no tips on this one. ... > Searching the newsgroups shows that the proper ... what is the standard method for rewinding? ...
    (comp.lang.cpp)