Reading floats from a file in C++

Started by
15 comments, last by the_cyberlord 19 years, 6 months ago
Why does this give an endless loop?

GLint loader(vector<GLfloat> &model )
{
ifstream f_in;
// open file
f_in.open("rawtri.raw", ios_base::in);

// go through whole file
while (!f_in.eof())
{
GLfloat f;

// read value, you don't need to care about spaces, tabs or newlines
f_in >> f;

// add value to vector
model.push_back(f);
cout << "Added coordinate";
}

// close file
f_in.close();

// write values to output
for (vector<GLfloat>::iterator it = model.begin(); it != model.end(); ++it)
{
cout << *it << endl;
}

return 0;
}
Advertisement
Quote:Original post by the_cyberlord
Why does this give an endless loop?

GLint loader(vector<GLfloat> &model )
{
ifstream f_in;
// open file
f_in.open("rawtri.raw", ios_base::in);

// go through whole file
while (!f_in.eof())
{
GLfloat f;

// read value, you don't need to care about spaces, tabs or newlines
f_in >> f;

// add value to vector
model.push_back(f);
cout << "Added coordinate";
}

// close file
f_in.close();

// write values to output
for (vector<GLfloat>::iterator it = model.begin(); it != model.end(); ++it)
{
cout << *it << endl;
}

return 0;
}


GLint loader(vector<GLfloat>& model ) {   std::ifstream ifs("rawtri.raw");   std::copy(std::istream_iterator<float>(ifs),             std::istream_iterator<float>(),             std::back_inserter(model));   ifs.close();   std::copy(model.begin(), model.end(),             std::ostream_iterator<float>(std::cout, ", "));   return 0;}


are you sure your reading a text file?
well, yeah, if you open it with notepad you see this:
5.511850 5.511848 0.000000 -5.511847 5.511851 0.000000 -5.511850 -5.511850 0.000000
-5.511850 -5.511850 0.000000 5.511850 -5.511851 0.000000 5.511850 5.511848 0.000000
if I save it as a txt file, it still acts like there's no end at the file
and this is the console output of my program
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
Press any key to continue
as you see, it doesn't read the right values, and it only reads the first value.
I limited it to read just 18 values, because if 'file end' is used, he falls in that endless loop
well i just tested both versions of code and they both work with this input file:

 5.511850  5.511848  0.000000 -5.511847  5.511851  0.000000 -5.511850 -5.511850  0.000000-5.511850 -5.511850  0.000000  5.511850 -5.511851  0.000000  5.511850  5.511848  0.000000


maybe you should tells us what compiler your using & post all your code in source tags in here.
I see you are having problems with my code. Hmm, it's strange, because I tested it before I posted here and it worked ok.
What compiler do you use?
VC++ 6...

This topic is closed to new replies.

Advertisement