T
Thomas Matthews
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?
--
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
http://www.sgi.com/tech/stl -- Standard Template Library
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?
--
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
http://www.sgi.com/tech/stl -- Standard Template Library