A quick question: I have been writing a small program to simply read a file and send to cout.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
char ch;
ifstream in_stream;
in_stream.open("self.cpp");
while(!in_stream.eof()) {
cout << ch;
in_stream.get(ch);
}
in_stream.close();
return 0;
}
This works ok, except that the last '}' of the file is printed twice.
However, if I reverse the order of lines 11 & 12 (so that in_stream.get is called before cout << ch), the last character of the file is printed only once so the program works correctly.
I cannot figure out the reason for this subtle difference. Could anybody help me?
Thanks.