从文件中读取对象数据并将其存储在向量中

问题描述

我有一个包含对象信息的文件,例如位置,旋转和缩放。我想阅读此信息并将其存储在三个数组中。我只想读取文件中的每个字符一次。

我的文件如下:

[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

我使用fstream打开文件,我也使用这三个向量来存储数据。

std::vector<glm::vec3> positions
std::vector<glm::vec3> rotaitons
std::vector<glm::vec3> scales

这是我的主循环:

while(std::getline(file,line)){
   if(line == "[Positions]"){
      // read positions
   }
   
   if(line == "[Rotations]"{
      // read Rotations
   }
   ...
}

我尝试了以下操作:

int x,y,z;
file >> x >> y >> z;
// write x,z to array

我也尝试过:

std::getline(file,line);
while(line != ""){
   // read x,z from line
   std::getline(file,line); // new line
}

以上方法有效,但是我不喜欢它,因为我先读了一行然后进行了解析。我想阅读直到有条件并停止为止。上面的选项几乎是我想要的,但是问题是我不知道何时停止调用它。 file >> x >> y >> z;读到最后一行,在调用之后调用file.get()std::getline(file,line),总是返回""

我看过以下内容:

if(file >> content && condition)

这是合乎逻辑的,我想知道是否可以做这样的事情:

while(file >> content != ""){
   // use content
}

如果可以执行以下操作:

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.

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)