如何将数据从文本文件读取到结构数组中 [英] How do I read data from a text file into an array of struct

查看:61
本文介绍了如何将数据从文本文件读取到结构数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从名为 fields.txt 的文本文件中读取数据,该文本文件包含 struct Fields 的成员.

I'm trying to read data from a text file called fields.txt which holds the members of the 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} 

我希望程序将数据读入名为 Fields fielddata [8] = {}; 的结构数组中,以便能够使用数据创建显示.

I want my program to read the data into my array of struct called 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;
}

推荐答案

要专门处理OP帖子中显示的输入类型(外面带有花括号的逗号分隔符),请执行此操作.想法取自此线程

To specifically deal with the type of input shown in OP's post (comma delimiters with curly braces outside), this is what is done. 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. 程序从文件中读取一行并将其放入 String行(直到不再有要读取的行[EOF]).
  2. inputstringstream 用空格替换所有出现的逗号和花括号,以便于获取.
  3. 然后,我们使用向量获取 iss 中剩余的所有数字.
  4. 然后,为 struct字段数据的每个成员赋予向量中每个相关位置的值.由于向量的类型为 float .我们将前两个转换为整数.
  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]).
  2. An inputstringstream replaces all occurences of commas and curly braces with spaces, for easy acquisition.
  3. We then use a vector to get all the numbers left over in iss.
  4. Each member of the struct fielddata is then given the value in each relevant position in the vector. We convert the first two to integers since the vector is of type float.


来自此线程

首先,创建一个 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)
   }

您只需要创建5个变量,这些变量与您要放入每个结构中的数据类型相匹配.例如.2个 int s,3个 float s.然后,您只需遵循上面概述的格式,然后将每个变量分配给该结构的每个成员.

You'll simply need to create 5 variables which match the data types you're looking for to put into each struct. E.g. 2 ints, 3 floats. Then you simply follow the format outlined above and assign each variable to each member of the struct.

此外,建议在main的开头而不是在中间初始化所有变量.

Also, it would be advisable to initialize all your variables at the start of main and not in the middle.

还有一点帮助可以推动您前进.

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.

这篇关于如何将数据从文本文件读取到结构数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆