0

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.

Bernd Eissing
  • 29
  • 1
  • 5
  • If I do `while (file >> x >> y >> z)` I cannot use `std::getline(file, line)` after and my loop stops after reading the Positions. My main loop is `while(std::getline(file, line))` becuase I need to differentiate between Positions, Rotations, Scales. – Bernd Eissing Oct 20 '20 at 14:51
  • 1
    Not sure what you don't like about parsing but you could use `std::stringstream`: Initialize this with `line` and then use the stream's `operator>>` to just read it like you read `fstream`. – Daniel Junglas Oct 20 '20 at 15:04
  • It's a good idea and it works, thank you. I just don't like the idea of reading/parsing every line twice. Assuming I have alot more Objects this feels wasted. Maybe the solution for me is to read character by character when I hit a section and stop when I hit `""`. I was hoping there would be a more elegant solution. – Bernd Eissing Oct 20 '20 at 15:21
  • @BerndEissing "*If I do `while (file >> x >> y >> z)` I cannot use `std::getline(file, line)` after*" - see [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/). – Remy Lebeau Oct 20 '20 at 16:27
  • Hey, thanks for the link, very insightful. I understand the problem now, once I have a solution I'll post it. – Bernd Eissing Oct 20 '20 at 16:57

0 Answers0