I have a file containing Object information such as Position, Rotation and Scale. I would like to read this information and store it inside three arrays. I would like to read every character in my file only once.
My file looks like this:
[Positions]
1 5 0
1 -5 0
0 0 0
[Rotations]
90.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
[Scales]
1 1 1
1 1 1
1 1 1
I use fstream to open the file, I also use these three vectors in which I want to store the data.
std::vector<glm::vec3> positions
std::vector<glm::vec3> rotaitons
std::vector<glm::vec3> scales
This is my main loop:
while(std::getline(file, line)){
if(line == "[Positions]"){
// read positions
}
if(line == "[Rotations]"{
// read Rotations
}
...
}
I tried the following:
int x, y, z;
file >> x >> y >> z;
// write x, y, z to array
I also tried this:
std::getline(file, line);
while(line != ""){
// read x, y, z from line
std::getline(file, line); // new line
}
The above works but I don't like it because I read a line and then parse it. I would like to read until a condition and stop. The above option is almost what I want but the problem is I don't know how when to stop calling it. file >> x >> y >> z;
reads the line until the end, calling file.get()
or std::getline(file, line)
after such a call will return ""
, always.
I have seen the following:
if(file >> content && condition)
This is logical AND, I'm wondering if I could do something like this:
while(file >> content != ""){
// use content
}
Of if something like this could be done:
while(file >> content (BITWISE NOT) -1){
// use content
}
I would then use `-1` to indicate the end of the data in my file instead of `""`.
So far I have not been able to get it to work, any help would be apriciated.