代码之家  ›  专栏  ›  技术社区  ›  Cristian Gira

解释为什么它不返回它应该返回的内容[关闭]

c++
  •  -4
  • Cristian Gira  · 技术社区  · 7 年前

    我有一个从“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();
    }
    

    在“curent.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。

    提前谢谢。

    1 回复  |  直到 7 年前
        1
  •  2
  •   NiVeR    7 年前

    你应该关闭 inputstream 一旦你读完了所有的数据,那么就脱离了循环,而不是内部。