文件IO错误地循环

时间:2013-12-06 03:45:50

标签: c++ file-io

我有一个readFile函数,它应该从文本文件中识别学生的类型。如果其国籍是本地的,它将创建一个localstudent类并将所有细节存储到localstudent对象和push_back到向量中。同样适用于国际学生。当我使用cout检查向量的大小增加时,它会显示大小增加3倍,这意味着我的函数循环3次。我该如何解决这个问题?

在文件中输入

Local   Bob 1 12 2003   Female  3   34  5345    345 3131    Bob 12313   1 12 2003   12 12 2000  1231    3   3   4   115.476 1346.48 258.75  4   234
Local   Tom 1/12/2003   male    3   34  5345    345 3131    Tom 12313   1/12/2003   12/12/2000  1231    3   3   4   115.476 1346.48 258.75  4   234

输出将显示Bob一次和Tom两次的详细信息。我知道in.eof()是错误的,但我不知道为此循环的任何其他方法。

void readFile(vector <clsStudent*> &s)
{
    string name, IC_no,state, gender, accNumb, accName, progName, ReadNationality, progCode, passportNumber, nation, visa, scholarship;
    int accID, times;
    float GPA, discountedFees, principle, rate, years, CI, C_Amount, progFees;
    clsDate DOB, accStart, accDOB, expDate;

    string line;
    ifstream in(filename);
    while(in)
    {

       if(in)
        {
            in >> ReadNationality;
            in >> name;
            in >> DOB;
            in >> gender;
            in >> GPA;
            in >> progName;
            in >> progCode;
            in >> progFees;
            in >> accNumb;
            in >> accName;
            in >> accID;
            in >> accDOB;
            in >> accStart;
            in >> principle;
            in >> rate;
            in >> years;
            in >> times;
            in >> CI;
            in >> C_Amount;
            in >> discountedFees;

            if (ReadNationality == "Local")
            {
                clsLocalStudent *stud;
                stud = new clsLocalStudent();
                in >> IC_no;
                in >> state;
                clsUniversityProgram *program= new clsUniversityProgram(progName, progCode, progFees);
                clsAccount objAccount(accNumb,name,accID, accDOB, accStart, principle, rate, years, times);

                stud->setName(name);
                stud->setDateOfBirth(DOB);
                stud->setGender(gender);
                stud->setNationality(ReadNationality);
                stud->setGPA(GPA);
                stud->setProgram(program);
                stud->setAccounts(objAccount);
                stud->setIC(IC_no);
                stud->setState(state);
                s.push_back(stud);
            }
            else
            {
                clsInternationalStudent *stud;
                stud = new clsInternationalStudent();
                in >> passportNumber;
                in >> nation;
                in >> visa;
                in >> expDate;
                in >> scholarship;
                clsUniversityProgram program(progName, progCode, progFees);
                clsAccount objAccount(accNumb,name,accID, accDOB, accStart, principle, rate, years, times);

                stud->setName(name);
                stud->setDateOfBirth(DOB);
                stud->setGender(gender);
                stud->setNationality(ReadNationality);
                stud->setGPA(GPA);
                stud->setProgram(&program);
                stud->setAccounts(objAccount);
                stud->setPassportNo(passportNumber);
                stud->setNation(nation);
                stud->setStudentVisa(visa);
                stud->setExpiryDate(expDate);
                stud->setScholarship(scholarship);
                s.push_back(stud);
            }
        }
        else
        cerr << "\nCannot open " << filename << " for reading" << endl;
    cout << s.size();
    }



    in.close();
}

void operator >> (istream &is, clsDate &date)
{
    string inputDate;
    int dd, mm, yy;
    char separator;

    is >> dd >> separator >> mm >> separator >> yy;

    date.setSeparator(separator);
    date.setMM(mm);
    date.setDD(dd);
    date.setYY(yy);
}

2 个答案:

答案 0 :(得分:3)

您期待while (in),而冗余的if (in)可以预测未来,并以某种方式保证未来的读取能够正常运行。当然,他们不能这样做。相反,检查每个输入操作以确保它成功,并且一旦失败,就中止循环。

最简单的方法可能是在上次读取之后以及处理任何读取的数据之前检查in 的状态。

答案 1 :(得分:0)

更改readFile()功能,如下所示

void readFile(vector <clsStudent*> &s)
{
    string name, IC_no,state, gender, accNumb, accName, progName, ReadNationality, progCode, passportNumber, nation, visa, scholarship;
    int accID, times;
    float GPA, discountedFees, principle, rate, years, CI, C_Amount, progFees;
    clsDate DOB, accStart, accDOB, expDate;

    string line;
    ifstream in(filename);
    if (in.is_open())
    {
        while(in >> ReadNationality >> name
            >> DOB >> gender >> GPA
            >> progName >> progCode >> progFees
            >> accNumb>> accName>> accID
            >> accDOB>> accStart>> principle
            >> rate>> years>> times
            >> CI>> C_Amount>> discountedFees
        )
        {
            if (ReadNationality == "Local")
            {
                clsLocalStudent *stud;
                stud = new clsLocalStudent();
                in >> IC_no;
                in >> state;
                clsUniversityProgram *program= new clsUniversityProgram(progName, progCode, progFees);
                clsAccount objAccount(accNumb,name,accID, accDOB, accStart, principle, rate, years, times);

                stud->setName(name);
                stud->setDateOfBirth(DOB);
                stud->setGender(gender);
                stud->setNationality(ReadNationality);
                stud->setGPA(GPA);
                stud->setProgram(program);
                stud->setAccounts(objAccount);
                stud->setIC(IC_no);
                stud->setState(state);
                s.push_back(stud);
            }
            else
            {
                clsInternationalStudent *stud;
                stud = new clsInternationalStudent();
                in >> passportNumber;
                in >> nation;
                in >> visa;
                in >> expDate;
                in >> scholarship;
                clsUniversityProgram program(progName, progCode, progFees);
                clsAccount objAccount(accNumb,name,accID, accDOB, accStart, principle, rate, years, times);

                stud->setName(name);
                stud->setDateOfBirth(DOB);
                stud->setGender(gender);
                stud->setNationality(ReadNationality);
                stud->setGPA(GPA);
                stud->setProgram(&program);
                stud->setAccounts(objAccount);
                stud->setPassportNo(passportNumber);
                stud->setNation(nation);
                stud->setStudentVisa(visa);
                stud->setExpiryDate(expDate);
                stud->setScholarship(scholarship);
                s.push_back(stud);
            }
        }
        cout << s.size();
        in.close();
    }
    else
        cerr << "\nCannot open " << filename << " for reading" << endl;

}