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:
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)
or
while(file.good())
i also have printed the get pointer with file.tellg() and always get -1. Lastly, i tried reading one line, then created a stringstream and read from it:
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:
I get the following:
3036679276 4677957
3233819949 6457113
3602920021 7347886
3602920021 7347886
2487223488 3069408
2818574689 4363018
2818574689 4363018
2818574689 4363018
2818574690 4363020
2818574690 4363020
2818574690 4363020
It looks like the seek internal pointer of fstream is not moving. Any clues about this? I'm in RH with GCC3.4. ????? I don't get it :S Any ideas??? I'm starting to believe that my computer is possessed