C 如何将文件解析到结构的向量中

c++ How to parse a file into vector of structs

本文关键字:结构 向量 文件      更新时间:2023-10-16

我有一个带有数据格式的文件,如下所示:

a 1.000 -1.000 1.000

b 7.89 4.56 2.46

C 50 20 10

我开始编写一些代码来解析文件并将数据存储在结构的向量中,但是我不确定如何完成此操作。

struct Coordinates
{
    double x;
    double y;
    double z;
}
vector<Coordinates> aVec; 
vector<Coordinates> bVec;  
vector<Coordinates> cVec;  
ifstream exampleFile;
exampleFile.open("example.txt");
// parse file
while(getline(exampleFile, str))
{
    if(str[0] == "a")
    {
        Coordinates temp;
        temp.x = firstNum; // this is where I'm stuck
        temp.y = secondNum;
        temp.z = thirdNum;
        vertVec.push_back(temp);
    }
    if(str[0] == "b")
    {
        Coordinates temp;
        temp.x = firstNum;
        temp.y = secondNum;
        temp.z = thirdNum;
        vertVec.push_back(temp);
    }
    if(str[0] == "c")
    {
        Coordinates temp;
        temp.x = firstNum;
        temp.y = secondNum;
        temp.z = thirdNum;
        vertVec.push_back(temp);
    }
}

首先说明文件表示为流。
流只是您可以阅读的内容。因此,您需要为您的结构编写流操作员,以便可以阅读它们。

 std::ifstream    file("Data");   // This represents a file as a stream
 std::cin                         // This is a stream object that represents
                                  // standard input (usually the keyboard)

他们都从std::istream继承。因此,当传递给使用流的函数时,它们都相同。

 int value;
 std::cin >> value;   // The >> operator reads from a stream into a value.

首先编写结构,以便知道如何从流中读取自身。

struct Coordinates
{
    double x;
    double y;
    double z;
    // This is an input function that knows how to read 3
    // numbers from the input stream into the object.
    friend std::istream& operator>>(std::istream& str, Coordinates& data)
    {
         return str >> data.x >> data.y >> data.z;
    }
}

现在编写一些读取类型坐标对象的代码。

int main()
{
     // Even if you only have a/b/c using a map to represent these
     // values is better than having three different vectors
     // as you can programmatically refer to the different vectors
     std::map<char, std::vector<Coordinates>>    allVectors;

     char         type;
     Coordinates  value;
     // Read from the stream in a loop.
     // Read the type (a/b/c)
     // Read a value (type Coordinates)
     while(std::cin >> type >> value)
     {
         // If both reads worked then
         // select the vector you want and add the value to it.
         allVectors[type].push_back(value);
     }
}

而不是使用

if(str[0] == "a")
{
    Coordinates temp;
    temp.x = firstNum; // this is where I'm stuck
    temp.y = secondNum;
    temp.z = thirdNum;
    vertVec.push_back(temp);
}

使用以下内容:

// Construct a istringstream from the line and extract the data from it
std::istringstream istr(str);
char token;
Coordinates temp;
if ( istr >> token >> temp.x >> temp.y >> temp.z )
{
   // Use the data only if the extractions successful.
   if ( token == 'a' ) // Please note that it's not "a"
   {
      // Do the right thing for a
   }
   // Add other clauses.
}
else
{
   // Deal with the error.
}