简体   繁体   English

如何将数据从文本文件读取到结构数组中

[英]How do I read data from a text file into an array of struct

I'm trying to read data from a text file called fields.txt which holds the members of the struct Fields . 我正在尝试从名为fields.txt的文本文件读取数据,该文本文件包含struct Fields的成员。

{1, 0, 7.4, 39.5, 5.33784}, 
{3, 1, 4.6, 27.9, 6.06522}, 
{5, 2, 2.2, 12.5, 5.68182}, 
{8, 0, 14.5, 86, 5.93103}, 
{11, 1, 8, 43.8, 5.475}, 
{16, 2, 5.9, 37.7, 6.38983}, 
{22, 0, 12.7, 72, 5.66929}, 
{24, 1, 10.5, 63.2, 6.01905} 

I want my program to read the data into my array of struct called Fields fielddata[8] = {}; 我希望程序将数据读入称为Fields fielddata[8] = {};的结构数组中Fields fielddata[8] = {}; so that I am able to use the data to create a display. 这样我就可以使用数据创建显示。

#include<iostream>
#include<fstream> 

using namespace std;
std::ifstream infile("fields.txt");

int initialise(int field, int crop, float size, float yof, float yph);

struct Fields {


int Field;
int Crop;
float Size;
float Yof;
float Yph;

int initialise(int field, int crop, float size, float yof, float yph)
{
    Field = field;
    Crop = crop;
    Size = size;
    Yof = yof;
    Yph = yph;

};

};



int main() {


Fields fielddata[8];

ifstream file("fields.txt");
if(file.is_open())
{


    int a, b, i = 0;
    float c, d, e;
    while (infile >> a >> b >> c >> d >> e)
    {
        fielddata[i].Field = a;
        fielddata[i].Crop = b;
        fielddata[i].Size = c;
        fielddata[i].Yof = d;
        fielddata[i].Yph = e;

        ++i;
    }


}




int highyph = 0;



cout << "Field\t" << "Crop\t" << "Size\t" << "YOF\t" << "YPH\t" << endl;

for (int i = 0; i < 8; i++) {


    cout << fielddata[i].Field << "\t" << fielddata[i%3].Crop << "\t" << fielddata[i].Size << "\t" << fielddata[i].Yof << "\t" << fielddata[i].Yph << "\t" << endl;
}


for (int i = 0; i < 8; i++)
{
    if (fielddata[i].Yph > highyph)
        highyph = fielddata[i].Field;
}

cout << "The Field with the Highest Yield is " << highyph << endl;




system("Pause");
    return 0;
}

Edit: To specifically deal with the type of input shown in OP's post (comma delimiters with curly braces outside), this is what is done. 编辑:要专门处理OP帖子中显示的输入类型(外面有花括号的逗号分隔符),就可以完成此操作。 Idea taken from this thread 这个主意

//Get each line and put it into a string
String line;
while (getline(infile, line)) {
     istringstream iss{regex_replace(line, regex{R"(\{|\}|,)"}, " ")};
     vector<float> v{istream_iterator<float>{iss}, istream_iterator<float>{}};

     //Assigns each member of the struct to a member of the vector at the relevant position
     fielddata[i].Field = static_cast<int>(v.at(0));
     fielddata[i].Crop = static_cast<int>(v.at(1));
     fielddata[i].Size = v.at(2);
     fielddata[i].Yof = v.at(3);
     fielddata[i].Yph = v.at(4);
     ++i;
}

Basically what's happening here is: 基本上,这里发生的是:

  1. The program reads a line from the file and puts it into the String line (until there are no more lines to be read [EOF]). 程序从文件中读取一行并将其放入String line (直到不再有要读取的行[EOF])。
  2. An inputstringstream replaces all occurences of commas and curly braces with spaces, for easy acquisition. 输入inputstringstream将所有出现的逗号和花括号替换为空格,以便于获取。
  3. We then use a vector to get all the numbers left over in iss . 然后,我们使用向量获取iss剩余的所有数字。
  4. Each member of the struct fielddata is then given the value in each relevant position in the vector. 然后,在向量的每个相关位置给struct fielddata每个成员一个值。 We convert the first two to integers since the vector is of type float . 由于向量的类型为float将前两个转换为整数。

From this thread 这个线程

First, make an ifstream : 首先,创建一个ifstream

 #include <fstream> std::ifstream infile("thefile.txt"); 

Assume that every line consists of two numbers and read token by token: 假设每一行包含两个数字,并逐个令牌读取令牌:

  int a, b; while (infile >> a >> b) { // process pair (a,b) } 

You'll simply need to create 5 variables which match the data types you're looking for to put into each struct. 您只需要创建5个变量即可与您要放入每个结构的数据类型匹配。 Eg 2 int s, 3 float s. 例如2 int s,3 float s。 Then you simply follow the format outlined above and assign each variable to each member of the struct. 然后,您只需遵循上面概述的格式,并将每个变量分配给该结构的每个成员。

Also, it would be advisable to initialize all your variables at the start of main and not in the middle. 同样,建议在main的开头而不是在中间初始化所有变量。

And one more bit of help to nudge you along. 还有一点帮助可助您一臂之力。

    int a, b, i = 0;
    float c, d, e;
    while (infile >> a >> b >> c >> d >> e)
    {
        fieldData[i].Field = a;
        //Assign other struct members as you wish
        ++i; //Or use an inner for loop to do the incrementation
    }

If you need further guidance with this let me know, but I'd like to see what you manage to do with it. 如果您需要进一步的指导,请告诉我,但我想看看您如何处理它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM