1

I am e c++ beginner. I have a 6 columns and 6 million rows text file of numerical integer values (up to 12 digits) and I must compare, line by line, numbers in col. 1, 3 and 5: they should be the same number: if not, the code shuold send me a warning specifying the row. I tried using this code:

 int main() {
     int time1, time2, time3, n1, n2, n3;
     string fileinp;
     cout << "input file: "; 
     cin >> fileinp;
     int nlines = 0;
     ifstream f1(fileinp.c_str());
     while (!f1.eof()) {
        nlines = nlines+1;
        f1 >> time1 >> n1 >> time2 >> n2 >> time3 >> n3;
        //cout << nlines << "  " << time1 << endl;
        if(time1 != time2 || time2 != time3 || time1 != time3)  {
        cout << "timestamp not corresponding for event n. " << nlines << endl;
     }
   }
   cout << nlines << endl;
   f1.close();
 }

but the program always reads the first line: how can I go to the next line in the "while" loop? Thanks

corvi
  • 11
  • 2
  • `while (!f1.eof())` is a bug. Read more here: [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301 Oct 26 '16 at 22:19
  • Save yourself a bit of logic. if `time1 == time2` and `time2 == time3` then you know `time1 == time3` – user4581301 Oct 26 '16 at 22:22
  • 12 digits may not fit in an `int`. A 32 bit `int` will only hold up to 2,147,483,647, or 10 digits. [Ensure you have a 64 bit integer with one of these](http://en.cppreference.com/w/cpp/types/integer). – user4581301 Oct 26 '16 at 22:26
  • You have to specify how columns are separated in the file. >> operator can only read space separated value – alangab Oct 26 '16 at 22:27
  • Answer 1 option 2 (Line-based parsing, using string streams) to [Read file line by line](http://stackoverflow.com/questions/7868936/read-file-line-by-line) should help you enormously. – user4581301 Oct 26 '16 at 22:28

0 Answers0