解释为什么它不返回它应该返回的内容

Explanation why it doesn't return what it should

本文关键字:返回 解释 为什么      更新时间:2023-10-16

我有一个从文件"Curent.txt"获取学生信息的功能。

这是结构:

struct students {
string CodSt;
string NumeSt;
string PrenSt;
string DenDisc1;
string MedCD1;
string DenDisc2;
string MedCD2;
string DenDisc3;
string MedCD3;
} student[50];

这就是函数:

void getStudents() {
int i = 0;
ifstream ifs("Curenta.txt");
while(!ifs.eof()) {
ifs >> student[i].CodSt >> student[i].NumeSt >> student[i].PrenSt >> student[i].DenDisc1
>> student[i].MedCD1 >> student[i].DenDisc2 >> student[i].MedCD2 >> student[i].DenDisc3
>> student[i].MedCD3;
if(!ifs.eof()) {
i++;
cout << i;
}
var = i;
ifs.close();
}

而在"库伦特.txt"中,我只有这个:

9 8 1 1 6 1 1 1 1
3 1 1 1 4 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 7 1 1 1 1

我的问题是为什么当我输出变量"i"时,值只有 1。

提前谢谢。

读取完所有数据后,您应该关闭inputstream,因此不在循环中,而不是在循环中。