[ifstream] Double reading of the last character.


F

Felix.leg

Hi,

I have got a strange behavior in my fstream library, I suppose (I'm
using "g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3"). Even with a simple code
like this:
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main (int argc, char **argv)
{
ifstream afile("chars" , ifstream::in );
char z;

while( afile.good() ) {
afile.get(z);
cout << " '" << z << "' ";
}
cout << endl;
afile.close();
return 0;
}
It reads right until the end of file. Then it reads the last character
twice.

Is my library broken? Or it is me doing it wrong?
 
Ad

Advertisements

V

Victor Bazarov

I have got a strange behavior in my fstream library, I suppose (I'm
using "g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3"). Even with a simple code
like this:
Code:
#include<iostream>
#include<fstream>
using namespace std;
int main (int argc, char **argv)
{
	ifstream afile("chars" , ifstream::in );
	char z;

	while( afile.good() ) {
		afile.get(z);
		cout<<  " '"<<  z<<  "' ";
	}
	cout<<  endl;
	afile.close();
	return 0;
}
It reads right until the end of file. Then it reads the last character
twice.

Is my library broken? Or it is me doing it wrong?

See the FAQ.

V
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top