C++ 解释为什么它没有';I don’我不能归还它应该归还的东西

C++ 解释为什么它没有';I don’我不能归还它应该归还的东西,c++,C++,我有一个函数,可以从文件“Curent.txt”中获取关于学生的信息 这就是结构: struct students { string CodSt; string NumeSt; string PrenSt; string DenDisc1; string MedCD1; string DenDisc2; string MedCD2; string DenDisc3; string MedCD3; } student[50];

我有一个函数,可以从文件“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();
}
我的问题是,为什么当我输出变量“I”时,值只有1


提前感谢。

您应该在读取完所有数据后关闭
inputstream
,这样就可以在循环之外,而不是内部。

您希望看到什么
cout建议读取:。是的,你检查了两次文件的结尾,但是比起点击文件的结尾,还有更多的地方可能出错,这是一种更简单的方法,如链接中所述。噢,该死,谢谢,我没有注意到。
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