I have looked around a lot, and still not found how to do this, so, please bear with me.
Let's say i have to read a txt file that contains different kinds of data, where the first float is an id, and then there are a few (not always the same amount) of other floats representing other stuff... times, for example, in pairs.
So the file would look something like:
1 0.2 0.3
2.01 3.4 5.6 5.7
3 2.0 4.7
...
After a lot of research, I ended up with a function like this:
vector<Thing> loadThings(char* filename){
vector<Thing> things;
ifstream file(filename);
if (file.is_open()){
while (true){
float h;
file >> h; // i need to load the first item in the row for every thing
while ( file.peek() != '\n'){
Thing p;
p.id = h;
float f1, f2;
file >> f1 >> f2;
p.ti = f1;
p.tf = f2;
things.push_back(p);
if (file.eof()) break;
}
if (file.eof()) break;
}
file.close();
}
return things;
}
but the while loop with the (file.peek() != '\n')
condition never finishes by itself, i mean... peek never equals '\n'
does anybody have an idea why? Or perhaps some other way to read the file using >>
operators?!
Thank you very much!
just suggesting another way, why not use