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

From: Thomas Matthews (Thomas_MatthewsSpitsOnSpamBots_at_sbcglobal.net)
Date: 11/01/04


Date: Mon, 01 Nov 2004 16:22:01 GMT

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'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):
>
> #include <iostream>
> #include <fstream>
> #include <cstdlib>
> #include <string>
>
> using namespace std; // for this example.
>
> int main(void)
> {
> ifstream inp("my_data.txt");
> string text_line;
>
> // Read the entire data file
> // and send to cout.
> while (getline(inp, text_line))
> {
> cout << text_line << endl;
> }
>
> // Now, the inp file is in an error
> // state, so clear it before positioning.
> inp.clear();
>
> // At this point, G++ reports that inp is
> // in a good state {inp.good() == true}.
>
> // Rewind the file.
> inp.seekg(0);
>
> // At this point, G++ reports that inp is
> // in a failed state (inp.fail() == true).
> // However, Borland compiler reports that
> // inp is in a good state.
>
> // Clear the state after rewinding.
> // This is primarily for G++.
> inp.clear();
>
> // Read {the first} line of text.
> if (getline(inp, text_line))
> {
> cout << text_line << endl;
> }
> else
> {
> // The G++ compiler always comes here.
> cerr << "getline after rewind failed." << endl;
> }
>
> return EXIT_SUCCESS;
> }
>
> So, what is the standard method for rewinding?
> Which compiler is correct?
> Can somebody test with Microsoft?
>

If the platform is of any concern, I am using Win95.

-- 
Thomas Matthews
C++ newsgroup welcome message:
          http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq:   http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
          http://www.comeaucomputing.com/learn/faq/
Other sites:
     http://www.josuttis.com  -- C++ STL Library book


Relevant Pages

  • [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 ... I believe the correct method for rewinding is: ... neither sequence, the positioning operation fails. ...
    (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 ... I believe the correct method for rewinding is: ... neither sequence, the positioning operation fails. ...
    (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)