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();//关闭文件 }运行是文件写不进去,然后程序不动了就一直黑屏,求大佬指点
book *atemp; atemp=new book; infile>>(*atemp)
这么读文件是不对的,编译器不会自动把字符流转成book类型的。需要你自己读一行后,根据你定义的规则转换成book类型。
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632