从txt读取数据时系统自动多读取了一部分零碎的数据,希望大佬们能帮忙解决一下。

本人c++萌新,对c++还不太熟悉。(已解决,谢谢大家)

参考信息:【mie haha 的博客】转载请注明出处(万分感谢!):
https://blog.csdn.net/qq_40315080/article/details/88176331

图片说明(调试照片以及txt文档)

(修改如下,具体原因可见我转载的文章)图片说明

读取函数如下:


void Class::class_Read()    //从文件读入数据
{
    Date *p;
    p = ClassHead;
    ifstream in("class.txt");
    if (!in) { cout << "没有课程信息,请先录入课程信息!" << endl; return; }
    while (1)
    {
        string n,e;
        int s;
        double N,t;
        in >> N >> n >> t >> s >> e;
        class_insert(N,n,t,s,e);
        if(in.fail())
        {
            break;
        }
    }
    in.close();
}   



#endif;

in >> N >> n >> t >> s >> e;
之后马上检测fail();

    while (1)
    {
        string n, e;
        int s;
        double N, t;
        in >> N >> n >> t >> s >> e;
        if (in.fail()) {
            break;
        } else {
            class_insert(N, n, t, s, e);
        }
    }