简体   繁体   中英

While loop not reading entire ifstream file

My ReadFile function is not reading my entire file. The file has lines of information each relating to one student.

The function (or the while loop perhaps) is reading the first two records and then exits the loop. It is reading everything correctly but why does it not continue reading the entire file (which has 13 records in it)?

I've tried while(!infile.eof()) but the program doesn't even run with this.

Here is my ReadFile block:

void ReadFile() {// Reads data file into array

    ifstream infile;

    infile.open(cTextFileName);

    if (!infile.good()) {
        cout << "Cant find text data file!" << endl;
        exit(1);
    }

    int i = 0;
    int status;
    //bool endOfFile = infile.eof();

    infile >> gRecs[i].StudentNo;

    while (infile) {
        infile >> gRecs[i].FirstName;
        infile >> gRecs[i].LastName;
        infile >> gRecs[i].NumSubjects;
        //cout << "ENTERED WHILE LOOP" << endl;

        for (int j = 0; j < gRecs->NumSubjects; j++) {
            infile >> gRecs[i].Subjects[j].Code;
            infile >> status;

            if (status == 0) {
                gRecs[i].Subjects[j].Status == eEnrolled;
            } else if (status == 1) {
                gRecs[i].Subjects[j].Status == eProvisional;
            } else {
                gRecs[i].Subjects[j].Status == eWithdrawn;
            }


            infile >> gRecs[i].Subjects[j].Mark;

        }

        i++;
        infile >> gRecs[i].StudentNo;
    }

    gNumRecs = i;
    infile.close();
    infile.clear();

    cout << gNumRecs << " Records read!" << endl;
}
for (int j = 0; j < gRecs->NumSubjects; j++) {

应该

for (int j = 0; j < gRecs[i].NumSubjects; j++) {

In my opinion, the best solution to this problem is to just avoid it completely.

Instead of a single function that tries to read the entire file, I would write a function that just reads a single record. Then call that function repeatedly until the entire file has been read. To fit with the way the rest of the standard library works, the function to read a single record should be named operator>> . It should receive a reference an istream and a reference to a record, and return the reference to istream when it's done.

std::istream &operator>>(std::istream &is, gRec &record) {
    is >> record.FirstName;
    is >> record.LastName;
    is >> record.NumSubjects;
    for (int i=0; i<record.NumSubjects; i++) {
        is >> record.subjects[i].code;

        int raw_status;
        is >> raw_status;
        record.subject[i].status = cvt_status(raw_status);
        is >> record.mark;
    }
    return is;
}

Personally, I'd probably make a couple changes from there. I'd make a Status , Subject and Schedule , each of which defines its own operator>> to read itself from a file:

class Status {
    enum class status { enrolled, provisional, withdrawn } s;

    friend std::istream &operator>>(std::istream &is, Status &s) { 
        int i;
        is >> i;
        switch (i) { 
            case 1: s.s = status::enrolled;    break;
            case 2: s.s = status::provisional; break;
            case 3: s.s = status::withdrawn;   break;
        }
        return is;
    }
};

class Subject {
    int code;
    Status status;
    int mark;
public:
    friend std::istream &operator>>(std::istream &is, Subject &s) { 
        return is >> s.code >> s.status >> s.mark;
    }
};

class Schedule {
    std::vector<Subject> classes;
public:
    friend std::istream &operator>>(std::istream &is) {
        int num;
        is >> num;
        for (int i=0; i<num; i++) {
            Subject temp;
            is >> temp;
            classes.push_back(temp);
       }
       return is;
    }
};

Then a record would be something like this:

class Record {
    std::string FirstName, LastName;
    Schedule classes;
public:
    std::istream &operator>>(std::istream &is, Record &r) {
        return is >> r.FirstName >> r.LastName >> r.classes;
    }
};

And finally, reading an entire file full of student records would be something like this:

std::ifstream infile("filename");
std::vector<Record> records{std::istream_iterator<Record>(infile), {}};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM