可以在主函数中添加一个读取文本文档的程序,例如:
#include <fstream>
// 读取学生信息并添加到链表中
void readFromFile(Node* head, string fileName){
ifstream file(fileName); // 打开文件
if(file.is_open()){ // 判断文件是否成功打开
int id, age;
string name;
while(file >> id >> name >> age){ // 逐行读取文件中的每个学生信息
Student s = {id, name, age};
insertNode(head, s); // 将学生信息插入链表末尾
}
cout << "\n从文件中读取学生信息成功!\n";
}
else{
cout << "\n无法打开文件!\n";
}
}
int main(){
// 创建空链表
Node* head = createList();
int choice;
// 读取保存在"students.txt"文件中的学生信息并添加到链表中
readFromFile(head, "students.txt");
do{
// 打印菜单选项...
上述代码将会在程序运行时自动从”students.txt”文件中读取学生信息,并将其添加到链表中。如果无法打开该文件,则输出错误消息。




