• 其实就是个数据结构的基础作业,其中的从文件直接读入功能总是会有bug,后来查到是没有初始化cur指针,让其指向了NULL,所以造成后续的文件内容读入造成困难(此处特别感谢江荣哥的帮助@Aurora)
    下面就贴一下代码吧:
    存在错误的代码:

    template<class T>
    void List<T>::getInfo() {
    ifstream ifs("database.txt",ios::in);
    char ch;
    if (!ifs) { cerr << "读取数据失败!" << endl; exit(1); }
    ifs.get(ch);
    if (ifs.eof()) cout << "该文件尚未输入数据!" << endl;
    else {
    LinkNode<T>* cur = first->link ;
    first->link = cur;
    while (!ifs.eof()) {
    ifs.unget();
    char temp;
    ifs >> cur->data.stuId >> cur->data.stuName >> cur->data.stuBirthday >> cur->data.stuGender >> cur->data.stuBody>>temp;
    cout << "学号:" << cur->data.stuId << ' ' << "姓名:" << cur->data.stuName << ' ' << "生日:" << cur->data.stuBirthday << ' ' <<"性别:" << cur->data.stuGender << ' ' << "健康状况:" << cur->data.stuBody<<endl;
    LinkNode<T>* theNext = new LinkNode<T>;
    cur->link = theNext;
    cur = theNext;
    }
    cout << "读取成功" << endl;
    }
    }
    template<class T>
    void List<T>::getInfo() {
    ifstream ifs("database.txt",ios::in);
    char ch;
    if (!ifs) { cerr << "读取数据失败!" << endl; exit(1); }
    ifs.get(ch);
    if (ifs.eof()) cout << "该文件尚未输入数据!" << endl;
    else {
    LinkNode<T>* cur = new LinkNode<T>;
    first->link = cur;
    while (!ifs.eof()) {
    ifs.unget();
    char temp;
    ifs >> cur->data.stuId >> cur->data.stuName >> cur->data.stuBirthday >> cur->data.stuGender >> cur->data.stuBody>>temp;
    cout << "学号:" << cur->data.stuId << ' ' << "姓名:" << cur->data.stuName << ' ' << "生日:" << cur->data.stuBirthday << ' ' <<"性别:" << cur->data.stuGender << ' ' << "健康状况:" << cur->data.stuBody<<endl;
    LinkNode<T>* theNext = new LinkNode<T>;
    cur->link = theNext;
    cur = theNext;
    }
    cout << "读取成功" << endl;
    }
    }
  • 另外我感觉上面的关于文件读入的内容可以考虑江荣的那种while方法,即

    while(cur->data.stuId >>cur->data.stuName >> cur->data.stuBirthday >> cur->data.stuGender >> cur->data.stuBody>>temp)