philrosenberg
Technical User
I'm ripping my hair out over reading data in through an fstream.
I open the fstream and read the values in the file into a double then push them into a vector. Only problem is the values aren't being read.
is_open() returns true, but tellg() returns -1, and after trying to read in a value both the eofbit and failbit are set.
I am reusing the fstream object and the double after reading in data from another file. I've checked to make sure that the fstream is closed after its previous use, again using is_open(). The previous use works fine. I've tried renaming the file and recreating the file but it made no difference.
If anyone can think of why this should happen please let me know, a snippet of the code is below
double temp;
vector < double > separation;
int ypixels=673;
//some code to read in from other files
if(fin.is_open()) cout << "fin not closed" << endl;
fin.open("sep.txt", ios::in);
if(!fin.is_open())
{
cout << "cannot open sep.txt" << endl;
return 0;
}
cout << fin.tellg() << endl;
for(i=0; i<ypixels; i++)
{
fin >> temp;
if(fin.fail()) cout << "fail" << endl;
if(fin.eof()) cout << "eof" << endl;
if(fin.bad()) cout << "bad" << endl;
if(fin.good()) cout << "good" << endl;
separation.push_back(temp);
}
fin.close();
output is:
-1
fail
eof
I open the fstream and read the values in the file into a double then push them into a vector. Only problem is the values aren't being read.
is_open() returns true, but tellg() returns -1, and after trying to read in a value both the eofbit and failbit are set.
I am reusing the fstream object and the double after reading in data from another file. I've checked to make sure that the fstream is closed after its previous use, again using is_open(). The previous use works fine. I've tried renaming the file and recreating the file but it made no difference.
If anyone can think of why this should happen please let me know, a snippet of the code is below
double temp;
vector < double > separation;
int ypixels=673;
//some code to read in from other files
if(fin.is_open()) cout << "fin not closed" << endl;
fin.open("sep.txt", ios::in);
if(!fin.is_open())
{
cout << "cannot open sep.txt" << endl;
return 0;
}
cout << fin.tellg() << endl;
for(i=0; i<ypixels; i++)
{
fin >> temp;
if(fin.fail()) cout << "fail" << endl;
if(fin.eof()) cout << "eof" << endl;
if(fin.bad()) cout << "bad" << endl;
if(fin.good()) cout << "good" << endl;
separation.push_back(temp);
}
fin.close();
output is:
-1
fail
eof