c++ - 程序进入无限循环 C++

标签 c++ operator-overloading file-handling

下面是我重载“>>”运算符的程序

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

class Student{
    public :
        string name;
        string entry_no;
};

class Science : public Student{
    public :
    float marks;
    void create_file();
    void highest();

    friend istream& operator >> (istream& input, Science& stud);
};

istream& operator >> ( istream& input, Science& stud){
    input >> stud.name;
    input >> stud.entry_no;
    input >> stud.marks;
    return input;
}
void Science::create_file(){
    ifstream file_read;

    file_read.open("student.txt");

    ofstream file_write;
    file_write.open("science.txt");
    string line;

    while(!file_read.eof()){
        getline(file_read,line,'\n');

        if(line.find("Science") != string::npos){
            file_write << line;
            file_write << '\n';
        }
    }
}

class Art : public Student{
    public :
    string marks;
    void create_file();
    void highest();
    friend istream& operator >> (istream& input, Art& stud);
};

istream& operator >> ( istream& input, Art& stud){
    input >> stud.name;
    input >> stud.entry_no;
    input >> stud.marks;
    return input;
}

void Art::create_file(){
    ifstream file_read;

    file_read.open("student.txt");

    ofstream file_write;
    file_write.open("art.txt");
    string line;

    while(!file_read.eof()){
        getline(file_read,line,'\n');

        if(line.find("Art") != string::npos){
            file_write << line;
            file_write << '\n';
        }
    }
    file_read.close();
    file_write.close();
}

void find_marks(){

    string entry_no;
    cout << "Enter entry_no of the student to find marks " << endl;
    cin >> entry_no;

    ifstream file_read;
    file_read.open("science.txt");
    string stud_entry;
    Science stud;
    bool found = false;
    if(file_read.is_open()){
        cout << (file_read >> stud) << endl;
    while( file_read >> stud ){
        cout << "hi";
        if(!entry_no.compare(stud.entry_no)){
            cout << stud.marks << endl;
            found = true;
            break;
        }
    }
    }
    else
        cout << "error in openning"<< endl;

    if(!found)
        cout << "this student does not exist" << endl;
}

int main(){
    Science science_stud;
    Art art_stud;

    science_stud.create_file();
    art_stud.create_file();
    find_marks();   
    return 0;
}

如果 entry_no 不匹配,函数 find_marks() 中的 while 循环将进入无限循环。谁能解释为什么会这样?

最佳答案

eof() 测试仅在确定是否因为之前的转换失败而打印错误时真正有用。这是一个非常糟糕的循环条件:

  1. 不一定会达到。例如,如果转换在某个时候失败,流将进入失败状态,在状态被清除之前,它拒绝提取更多字符。
  2. std::ios_base::eofbit 标志在到达 EOF 时不设置(至少不保证设置)但它只保证在到达 EOF 时设置试图读取超过文件末尾的内容。因此,最后一组数据往往会被处理两次。

正确的方法是在读取数据后使用转换为bool:

while (file >> whatever) {
    ...
}

如果您的 C++ 教程建议您使用 eof(),您应该刻录它并建议其他人不要购买拷贝(他们需要刻录)。如果你的老师告诉你使用 eof() - ...好吧,据我所知,烧人已经过时了。你至少应该告诉他他错了。

关于c++ - 程序进入无限循环 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13221393/

相关文章:

python - 如何将 Stellarium 生成的文件的二进制编码文件转换为 ASCII

c++ - cl.h 是否适用于 C++?

c++ - re2 根本不改变字符串

c++ - += 运算符重载

templates - 用于模板化重载的不明确运算符<<

c - C 编程中文件处理中的 eof(f)

python - 更新文本文件python中的单个字段

c++ - 结合视频和音频流(Qt、OpenCV、PortAudio、libsnd?)

c++ - 如何强制 QGraphicsView/QGraphicsScene 缩小到最小尺寸

c++ - 严格弱序困惑