This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

question: is it a compiler bug or else?


Hi,

My name is Graziano Servizi, Associate Professor in Mathematical Physics at Bologna University, Italy, and I wrote to you some months ago, obtaining clean and quick answers.

So I would ask you again about some trouble I have with a sample code
I will propose to students for teaching purposes.

I read that the putback function of the basic_istream class would set
"badbit" on failure, is it right?

In the included code I called putback "on purpose" on a "readonly" input stream (an instance of the ifstream class) in a "modifying putback" context.

This shouldn't be allowed, I guess...

And indeed, after this call, the input stream turns out to be
"corrupted", despite the fact that rdstate() continues returning
"goodbit", thus preventing any reasonable check on it.

What is wrong in my code, which follows?
I'm using gcc 4.7.2 on a Linux Fedora 18 x86_64 system and the
problem occurs both with or without the -std=c++11 compiling option.
The content of the file "file" is whatever you want, provided it is
long enough: I used the 26 lowercase letters...

Thanks in advance.

Graziano Servizi


# include <iostream>
# include <fstream>
using namespace std;

int main( )
 {
  ifstream is("file");
  cout << "begins: " << is . tellg( ) << '\n';
  char c = is.get( );
cout << "after get " << is . tellg( ) << " with " << is . gcount( ) << " and c is [" << c << "]\n";
  is.seekg(2, ios::cur); // move ahead two bytes
cout << "after seekg " << is . tellg( ) << " with " << is . gcount( ) << '\n';
  cout << "STATE A " << is.rdstate() << '\n';
  is . putback(c); // shouldn't set badbit ??
  cout << "STATE B " << is.rdstate() << '\n';
// it turns out that badbit is not set (same output [0] in STATE A as in STATE B)
  // however...:
cout << "after putback " << is . tellg( ) << " with " << is . gcount( ) << " and next byte will be [";
  cout << (c = is.get( )) << "]\n";
}

// end of code



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]