C++ Simple file reading -
I have a file, named f1.txt, whose contents are 75 15 85 35 60 50 45 70 < / P>
My code is for reading and printing each integer.
#include & lt; Iostream & gt; # Include & lt; Fstream & gt; using namespace std; Int main (int argc, char * argv []) {fstream file ("f1.txt", ios :: in); Int i; While (! File.eof ()) {File> & Gt; I; Cout & lt; & Lt; I & lt; & Lt; ""; } Return 0; }
But when I compile and run the program, output is 75 15 85 35 60 50 45 70 70. Why is it reading the final integer twice? any clue?
Try:
while (file>; ) COT and LT; & Lt; I & lt; & Lt; "";
Comments
Post a Comment