This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
istream bug
- To: gcc at gcc dot gnu dot org
- Subject: istream bug
- From: Robert Schweikert <rjschwei at mindspring dot com>
- Date: Wed, 03 Jan 2001 22:02:25 -0500
When using the "ios::nocreate" opening mode on an istream the file
pointer moves to the end of the file which causes the read with the
stream operator ">>" to fail. I realize that using "ios::nocreate" on an
istream doesn't make a whole lot of sense, however, I think the compiler
should either ignore this opening mode on the istream or issue a warning
or error message. Putting the file pointer to the end of the file
produces behavior that is hard to track down.
The attached file illustrates the problem.
-> g++ -o strTestNb strTest.C
-> ./strTestNb
First Flag: 0
Second Flag: 1
Third Flag: 0
This is buffer: Assembly-1
This is n: 1
This is i: 0
->
-> g++ -o strTest -DDEMOBUG strTest.C
-> ./strTest
This is i: 66267
This is i: 66268
This is i: 66269
This is i: 66270
This is i: 66271
This is i: 66272
This is i: 66273
This is an endless loop.
A fix is appreciated.
Thanks,
Robert
--
Robert Schweikert MAY THE SOURCE BE WITH YOU
rjschwei@mindspring.com LINUX
#include <iostream.h>
#include <fstream.h>
int main(void)
{
#if defined(DEMOBUG)
ifstream input("strTest.prt", ios::nocreate);
#else
ifstream input("strTest.prt");
#endif
int flag;
int n;
int i;
char tail[132], buffer[132];
input >> flag;
cout << "First Flag: " << flag << endl;
input >> flag;
cout << "Second Flag: " << flag << endl;
input >> flag;
cout << "Third Flag: " << flag << endl;
input.getline(tail, 132); //Probably reads the end of the third line in the
//file
input.getline(buffer, 132);
cout << "This is buffer: " << buffer << endl;
input >> n;
cout << "This is n: " << n << endl;
for ( i = 0; i < n; i++) {
cout << "This is i: " << i << endl;
}
return 1;
}
0
1
0
Assembly-1
1