public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* question: is it a compiler bug or else?
@ 2013-11-18 15:47 Graziano Servizi
  0 siblings, 0 replies; only message in thread
From: Graziano Servizi @ 2013-11-18 15:47 UTC (permalink / raw)
  To: gcc-help

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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2013-11-18 15:29 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-18 15:47 question: is it a compiler bug or else? Graziano Servizi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).