【问题标题】:How do I read data from a text file into an array of struct如何将文本文件中的数据读入结构数组
【发布时间】:2016-02-07 23:50:33
【问题描述】:

我正在尝试从名为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} 

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

#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;
}

【问题讨论】:

标签: c++ arrays struct


【解决方案1】:

编辑:要专门处理 OP 帖子中显示的输入类型(外面带有花括号的逗号分隔符),这就是所做的。想法取自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;
}

基本上这里发生的事情是:

  1. 程序从文件中读取一行并将其放入String line(直到没有更多行要读取[EOF])。
  2. inputstringstream 将所有出现的逗号和大括号替换为空格,以便于获取。
  3. 然后我们使用向量来获取iss 中剩余的所有数字。
  4. 然后,struct fielddata 的每个成员在向量中的每个相关位置都被赋予了值。我们将前两个转换为整数,因为向量的类型为float

来自this thread

首先,创建一个ifstream

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

假设每一行由两个数字组成,逐个令牌读取:

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

您只需要创建 5 个与您要放入每个结构的数据类型相匹配的变量。例如。 2 个ints,3 个floats。然后,您只需按照上面概述的格式并将每个变量分配给结构的每个成员。

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

还有一点帮助来推动你前进。

    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
    }

如果您需要这方面的进一步指导,请告诉我,但我想看看您能用它做什么。

【讨论】:

  • 更新了我的帖子我到哪里去了@Keno Clayton?
  • @DanielH 看起来你在正确的轨道上,但我注意到 fielddata[i%3].Crop 是故意的吗?除此之外,它看起来应该可以工作,你测试了吗?
  • :@KanoClayton 是的,crop 数组中只有 3 个元素值(0,1,2),而其他数组中有 8 个。这就是我这样做的原因。我对其进行了测试,但在显示中得到了一堆奇怪的值,例如 -8503d304
  • @DanielH 听起来像一个变量没有初始化。仔细查看您的代码,我注意到您声明了两次 fielddata,这是不正确的。这是我的建议。从现在开始,在每个函数的开头声明所有变量。它可以帮助您跟踪所有变量。然后只需在需要使用它的任何地方调用该变量。
  • 好的,我已经改变了。其他人说过“您的代码没有考虑输入文件中的字符'{'、','和'}'”。有什么想法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-11
相关资源
最近更新 更多