problem with fstream

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ivotron
    New Member
    • Dec 2006
    • 6

    problem with fstream

    Hi,

    I have very strange problem with std::fstream. I want to read a simple two-column space-separated file with the following code:

    #include <iostream>
    #include <fstream>

    Code:
    int main()
    {
       std::ifstream file("file.txt");
       unsigned long oldval;
       unsigned long newval;
    
       while(!file.eof()) {
          file >> oldval;
          file >> newval;
          std::cout << oldval << " " << newval << std::endl;
       }
    }
    The file.txt contains ten lines only:

    Code:
    3036679276 4677957
    3233819949 6457113
    3602920021 7347886
    6236944672 16846074
    2487223488 3069408
    2818574689 4363018
    5419052468 13941073
    9172944933 31529903
    2818574690 4363020
    9172944938 31529909
    As output, I get an endless printing of the 3rd line. I have tried a lot of variants, like:

    Code:
    while(file>>oldval>>newval)
    while(file.good())
    and I always get the same behavior. It looks like the seek internal pointer of fstream is not moving. Any clues about this? I'm in RH with GCC3.4. Thanks for your time.
  • ivotron
    New Member
    • Dec 2006
    • 6

    #2
    Hi,

    Still trying to solve this. I'm trying reading each line and then, with std::stringstre am, pass

    Code:
    int main()
    {
       std::ifstream file("file.txt");
       unsigned long oldval;
       unsigned long newval;
       std::string line;
    
       while(!file.eof()) {
          std::getline(file, line);
          std::stringstream linestream(line);
          linestream >> oldval;
          linestream >> newval;
          std::cout << oldval << " " << newval << std::endl;
       }
    }
    I get the following:

    Code:
    3036679276 4677957
    3233819949 6457113
    3602920021 7347886
    3602920021 7347886
    2487223488 3069408
    2818574689 4363018
    2818574689 4363018
    2818574689 4363018
    2818574690 4363020
    2818574690 4363020
    2818574690 4363020
    ????? I don't get it :S Any ideas??? I'm starting to believe that my computer is possessed

    Comment

    • ivotron
      New Member
      • Dec 2006
      • 6

      #3
      Hi,

      Still trying to solve this... I've printed the put pointer, to know if its moving:

      Code:
      int main()
      {
         std::ifstream file("file.txt");
         unsigned long oldval;
         unsigned long newval;
      
         while(!file.eof()) {
            file >> oldval;
            file >> newval;
            std::cout << oldval << " " << newval << std::endl << file.tellg() << std::endl;
         }
      }
      I get always -1. I also reinstalled gcc.

      Comment

      Working...