A reading data irrelevantly

Hi All,

This script below reads a text file “map.txt”, which is in the attachment.
But Interestingly it reads an extra data. Specifically, I mean that it reads the last data one more time. Why ?

#include <iostream>
#include <fstream>
using namespace std;

void readfile() {

  fstream in("map.txt",ios::in);
    if (in.fail()){

      cout<<"there is no map.txt"<<endl;
    }

    int runno;
    int k=0;
    while(!(in.eof())) {

    in>>runno;                                                                                                                                                        
    cout<<""<<runno<<endl;
    }

}

Cheers,
Ersel
map.txt (241 Bytes)

while (in >> runno) { std::cout << runno << std::endl; }

1 Like

Thanks Pepe

So, Problem was that it reads the last data which is the end of file (eof) shown as /0 , wasn’t it ?

Cheers,
Ersel

while (1) { in >> runno; if (in.fail()) break; std::cout << runno << std::endl; }