int LibraryManager:: init_book(book **q)//从文件中读出图书信息,返回值用来中断函数 {int j=0;//用来计录读取信息数 ifstream infile("shuji.txt",ios::in);//从shuji.txt文件中读取数据 if(!infile) {cout<<"书籍文件不存在"<<endl; //书籍文件不存在 Sleep(3000); system("cls"); return 0; } while (!infile.eof()) {book *atemp; atemp=new book; infile>>(*atemp); if(infile.eof()){break;}//读取到文件尾,结束 j++; if(*q==NULL)//检查链表是否为空链表 {atemp->next=NULL; *q=atemp; } else {atemp->next=NULL; zbook->next=atemp; } zbook=atemp; } if(j==0){cout<<"书籍文件为空"<<endl; Sleep(3000); system("cls");} else {cout<<"从文件中读取数据"<<endl; Sleep(3000); system("cls"); } infile.close(); return 1; } void LibraryManager:: SaveData(book *q)//将图书数据保存在新文件中( (若文件不存在则新建) {book *m=q;book *btemp; fstream outfile("shuji.txt",ios::out);// shuji.txt if(!outfile) {cout<<"书籍文件保存失败"<<endl; exit (0); } while (m!=NULL)//链表不为空 {outfile<<*m;//把链表中数据输入文件保存,每保存一个释放一个 btemp=m; m=m->next; delete btemp;//释放链表空间 } outfile.close();//关闭文件 }运行是文件写不进去,然后程序不动了就一直黑屏,求大佬指点
2条回答 默认 最新
- qfl_sdu 2021-06-05 14:45关注
book *atemp; atemp=new book; infile>>(*atemp)
这么读文件是不对的,编译器不会自动把字符流转成book类型的。需要你自己读一行后,根据你定义的规则转换成book类型。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报