代码之家  ›  专栏  ›  技术社区  ›  mahmoudalsof

While循环不读取整个ifstream文件

  •  -1
  • mahmoudalsof  · 技术社区  · 6 年前

    函数(或者while循环)正在读取前两条记录,然后退出循环。它正在正确读取所有内容,但为什么它不继续读取整个文件(其中有13条记录)?

    我试过了 while(!infile.eof())

    这是我的ReadFile块:

    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;
    }
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   Hugo Maxwell    6 年前
    for (int j = 0; j < gRecs->NumSubjects; j++) {
    

    应该是

    for (int j = 0; j < gRecs[i].NumSubjects; j++) {
    
        2
  •  1
  •   Jerry Coffin    6 年前

    在我看来,解决这个问题的最好办法就是完全避免它。

    我将编写一个只读取一条记录的函数,而不是试图读取整个文件的单个函数。然后重复调用该函数,直到读取了整个文件。为了适应标准库其余部分的工作方式,应该将读取单个记录的函数命名为 operator>> . 它应该会收到一份推荐信 istream 以及对记录的引用,并返回对 峡流 当它完成的时候。

    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;
    }
    

    Status , Subject Schedule ,每个都定义了自己的 操作员>&燃气轮机; 要从文件中读取自身,请执行以下操作:

    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;
        }
    };
    

    那么一张唱片应该是这样的:

    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;
        }
    };
    

    最后,阅读一个完整的学生档案是这样的:

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