[英]How to read data from a file into a 1-D array of structures
我正在从事c ++程序的分配工作,我无法解决如何从文件中读取数据到一维结构数组中的问题。 我的数据文件如下所示:
01吉米·约翰逊(Jimmy Johnson)
190 60
02杰拉尔德·希尔
180 56
第一个数字是其ID,第二个字是其身高和体重。 我的老师要我将这些数据读入数组“宇航员”。 然后,使用一维结构数组以某种方式将正确的信息放入“名称”和“ BODYSTATS”中,以便以后可以正确打印该信息。 我只是将数据读入单个数组并进行打印。 我该如何在数组中将某些单词分配给某些(结构?)并在以后打印出来?
#include<string>
#include<fstream>
using namespace std;
struct ob
{
string name;
string id;
int height;
int weight;
};
int main()
{
ob p[100];
string emptyline;
int count=-1;
ifstream input("data.txt");
while (input>>p[++count].id)
{
getline(input, p[count].name);
p[count].name = p[count].name.substr(1);
input>>p[count].height>>p[count].weight;
getline(input, emptyline); // \n after weight
getline(input, emptyline); // empty line
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.