[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?
 
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
 

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

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top