需要将txt文件内的数据读取到程序中的结构体数组
但是我的方法都只能读入第一行数据 不知道为什么
20201100097 Eiuqhu 69 68 100
20201100298 Vyjap 83 88 100
20201100327 Kpvihj 64 96 100
20201100335 Kzygeefh 85 52 100
20201100351 Hlikid 57 31 100
20201100412 Fwfua 60 85 100
20201100621 Vfpatbv 88 99 100
20201100673 Sfaj 87 92 100
20201100749 Kqilg 82 85 100
20201100817 Tmh 91 99 100
20201101022 Sdbyr 74 41 100
20201101424 Oauucz 43 76 100
20201101523 Xygp 45 44 100
20201101554 Kaq 84 32 100
20201101600 Lrormn 65 83 100
20201101631 Krcvoqvk 98 39 100
20201101640 Wzpszlp 87 35 100
20201101721 Fyeur 57 86 100
20201101922 Tnk 65 62 100
20201101938 Eulv 35 39 100
20201101958 Qtzqr 38 89 100
20201101985 Wgapxmcn 91 61 100
20201102013 Tzsomhcu 80 90 100
20201102191 Pdyhxs 32 49 100
20201102298 Pgsmkyui 83 87 100
20201102344 Lrzcyu 47 54 100
20201102417 Pczj 85 55 100
20201102564 Cko 86 51 100
20201102589 Maj 90 34 100
20201102678 Kdffgb 48 78 100
我的错误尝试:
struct code
{
string num;
string name;
int a, b, c, t;
} ;
//这是结构体
void load_data(code data[])
{
ifstream in;
in.open("期末大作业data.txt", ios::in);
if(!in.is_open())
{
cout << "文件打开失败" << endl;
exit(1);
}
int i = 0;
while(!in.eof())
{
in >> data[i].num >> data[i].name >> data[i].a>> data[i].b >> data[i].c;
data[i].t = data[i].a + data[i].b + data[i].c;
i++;
}
in.close();
}
//这样输入只会读入第一行的数据
for循环也试过了
void load_data(code data[])
{
ifstream in;
in.open("期末大作业data.txt", ios::in);
if(!in.is_open())
{
cout << "文件打开失败" << endl;
exit(1);
}
int i = 0;
for(i; i < 10010; i++)
{
in >> data[i].num >> data[i].name >> data[i].a>> data[i].b >> data[i].c;
data[i].t = data[i].a + data[i].b + data[i].c;
}
in.close();
}
//这样子for循环也是只会读取第一行数据
请问各位我要怎么才能全部读入
函数中cnt没有赋值,末尾加一句 cnt = i;