为什么退出程序后文件中的信息无法导入链表!求助!!
比如进行注册后退出,然后再打开程序登陆,但登不了#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char title;
char author;
float price;
struct Date date;
char publisher;
struct Book*pnext;
};
struct USER
{
unsigned int id;
unsigned int password;
struct USER *pnext;
};
void read_bookfile(struct Book**pphead)
{
FILE*fp;
if((fp=fopen("图书信息.txt","r"))==NULL)
{
if((fp=fopen("图书信息.txt","w"))==NULL)
{
printf("打开文件失败!\n");
exit(EXIT_FAILURE);
}
fclose(fp);
if((fp=fopen("图书信息.txt","r"))==NULL)
{
printf("打开文件失败!\n");
exit(EXIT_FAILURE);
}
}
while(fgetc(fp)!=EOF)
{ struct Book *book=(struct Book*)malloc(sizeof(struct Book));
fscanf(fp,"书名:%s\n",book->title);
fscanf(fp,"作者:%s\n",book->author);
fscanf(fp,"售价:%f\n",&book->price);
fscanf(fp,"出版日期:%d-%d-%d\n",&book->date.year,&book->date.month,&book->date.day);
fscanf(fp,"出版社:%s\n\n",book->publisher);
book->pnext=*pphead;
*pphead=book;
}
fclose(fp);
}
void write_bookfile(struct Book*book)
{
FILE*fp;
if((fp=fopen("图书信息.txt","w"))==NULL)
{
printf("打开文件失败!\n");
exit(EXIT_FAILURE);
}
while(book!=NULL)
{
fprintf(fp,"书名:%s\n",book->title);
fprintf(fp,"作者:%s\n",book->author);
fprintf(fp,"售价:%.2f\n",book->price);
fprintf(fp,"出版日期:%d-%d-%d\n",book->date.year,book->date.month,book->date.day);
fprintf(fp,"出版社:%s\n\n",book->publisher);
book=book->pnext;
}
fclose(fp);
}
void read_userfile(struct USER**pphead)
{
FILE*fp;
int count=1;
if((fp=fopen("用户信息.txt","r"))==NULL)
{
if((fp=fopen("用户信息.txt","w"))==NULL)
{
printf("打开文件失败!\n");
exit(EXIT_FAILURE);
}
fclose(fp);
if((fp=fopen("用户信息.txt","r"))==NULL)
{
printf("文件打开失败!\n");
exit(EXIT_FAILURE);
}
}
while(fgetc(fp)!=EOF)
{
struct USER *user=(struct USER*)malloc(sizeof(struct USER));
fscanf(fp,"--第%d位用户信息--\n 账号:%u",&count,&user->id);
fscanf(fp,"密码:%u\n",&user->password);
user->pnext=*pphead;
*pphead=user;
}
fclose(fp);
}
void write_userfile(struct USER*user)
{
FILE*fp;
if((fp=fopen("用户信息.txt","w"))==NULL)
{
printf("打开文件失败!\n");
exit(EXIT_FAILURE);
}
while(user!=NULL)
{
int count=1;
fprintf(fp,"--第%d位用户信息--\n 账号:%u ",count,user->id);
fprintf(fp,"密码:%u\n",user->password);
user=user->pnext;
count++;
}
fclose(fp);
}
void print_log(void)
{
printf("\n\n----------------欢迎来到图书管理系统----------------------\n\n\n");
printf(" 1 登录\n");
printf(" 2 注册\n");
printf(" 3 退出\n\n\n");
printf(" ---请输入操作前的序号进行相应操作---\n\n");
}
void AddUser(struct USER**pphead)
{
struct USER*newuser=(struct USER*)malloc(sizeof(struct USER));
if(newuser==NULL)
{
printf("分配内存失败!\n");
exit(EXIT_FAILURE);
}
printf("--注册新账户--\n");
printf("账户:");
scanf("%u",&newuser->id);
printf("密码:");
scanf("%u",&newuser->password);
newuser->pnext=*pphead;
*pphead=newuser;
putchar('\n');
}
void log_on(unsigned int*id,unsigned int*password)
{
printf("——请输入账号和密码(未注册请先注册账户)——\n");
printf("账号:");
scanf("%u",id);
printf("密码:");
scanf("%u",password);
putchar('\n');
}
int confirm_user(struct USER*user,unsigned int id,unsigned int password)
{
while(1)
{
if(user==NULL) //要放前面
{
return -1;
}
if(id==user->id&&password==user->password)
{
return 1;
}
user=user->pnext;
}
}
int choose_num(struct USER**pphead,unsigned int *id,unsigned int *password)
{
int key=-1,kiss;
do{
printf("输入序号:");
scanf("%d",&kiss);
putchar('\n');
switch(kiss)
{
case 1:log_on(id,password);key=confirm_user(*pphead,*id,*password);
if(key!=1)
{
printf("账号或密码错误!\n\n");
}
break; //+break
case 2:AddUser(pphead);break;
case 3:write_userfile(*pphead);exit(1);break;
default:printf("输入指令错误!\n");
}
}while(key!=1);
return key;
}
void print_meau(void)
{
putchar('\n');
printf("%46c图书管理系统\n", ' ');
printf("\n\n");
printf("* * * * * * * * * * * * * * * * * * *\n");
printf("*1 录入图书 *\n");
printf("*2 打印已录入图书信息 *\n");
printf("*3 搜素图书 *\n");
printf("*4 删除已录入图书 *\n");
printf("*5 退出程序 *\n");
printf("* * * * * * * * * * * * * * * * * * *\n");
printf(" \n\n ---输入功能前的相应序号进行操作---\n\n");
}
void ReadBook(struct Book**pphead)
{
struct Book*book = (struct Book*)malloc(sizeof(struct Book));
printf("---开始录入书籍---\n");
getchar();
printf("请输入书名:");
gets(book->title);
printf("请输入作者:");
gets(book->author);
printf("请输入售价:");
scanf("%f", &book->price);
printf("请输入出版日期(按“ 2018-12-12 ”的格式输入):");
scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
getchar();//重要
printf("请输入出版社:");
//scanf("%s",book->publisher);
gets(book->publisher);
if(*pphead == NULL)
{
*pphead = book;
book->pnext = NULL;
}
else
{
struct Book*temp = *pphead;
while(1)
{
if(temp->pnext == NULL)
{
break;
}
temp = temp->pnext;
}
temp->pnext = book;
book->pnext = NULL;
}
}
void print_book(struct Book*phead)
{
struct Book *book = phead;
printf("\n---开始打印图书信息---\n\n");
if(phead == NULL)
{
printf("还未录入图书!\n");
}
else
{
while(1)
{
if(book == NULL)
{
break;
}
else
{
printf("书名:%s\n", book->title);
printf("作者:%s\n", book->author);
printf("售价:%.2f", book->price);
printf("出版日期:%d-%d-%d\n", book->date.year, book->date.month, book->date.day);
printf("出版社:%s\n", book->publisher);
putchar('\n');
book = book->pnext;
}
}
}
printf("\n---打印完毕---\n");
}
void scanf_search(char inform[])
{
printf("请输入搜索图书的的书名或作者:");
getchar(); //没有的话会异常退出
gets(inform);
}
struct Book*SearchBook(struct Book*book,char inform[])//搜索图书并返回指向该图书信息的指针,未找到则返回NULL
{
while(1)
{
if(book == NULL)
{
return book;
}
if(!strcmp(book->title, inform) || !strcmp(book->author, inform))
{
return book;
}
book = book->pnext;
}
}
void print_search(struct Book*book,char inform[]) //打印被搜索的图书信息
{
if(book == NULL)
{
printf("未找到图书信息!\n");
}
else
{
putchar('\n');
printf("---已找到被搜索图书,图书信息如下---\n\n");
do{
printf("书名:%s\n",book->title);
printf("作者:%s\n",book->author);
printf("售价:%.2f\n",book->price);
printf("出版日期:%d-%d-%d\n",book->date.year,book->date.month,book->date.day);
printf("出版社:%s\n",book->publisher);
putchar('\n');
}while((book=SearchBook(book->pnext,inform))!=NULL);
}
}
void cutoff_book(struct Book**pphead)//删除被搜索的图书信息 ,并释放内存
{
char cutbook;
struct Book *pervious = NULL;
struct Book *current = *pphead;
printf("请输入你要删除的图书的书名或作者:");
getchar();
gets(cutbook);
/* if(current == NULL)
{
printf("未找到需要删除的图书\n");
return; //返回
}*/
while(1)
{
if(current == NULL)
{
break;
}
if(!strcmp(current->title, cutbook) || !strcmp(current->author, cutbook))
{
break;
}
pervious = current;
current = current->pnext;
}
if(current == NULL)
{
printf("未找到需要删除的图书!\n");
}
else
{
if(pervious==NULL)
{
*pphead=current->pnext;
}
else
{
pervious->pnext=current->pnext;
}
free(current);
}
}
void releasebook(struct Book*book)//释放链表空间
{
struct Book *temp = NULL;
while(book != NULL)
{
temp = book;
book = book->pnext;
free(temp);
}
}
void releaseuser(struct USER*user)
{
struct USER *temp = NULL;
while(user!= NULL)
{
temp = user;
user = user->pnext;
free(temp);
}
}
int main(void)
{
int N,enter=-1;
char inform;
struct Book*phead = NULL;
struct USER *pheaduser=NULL;
unsigned int id,password;
read_bookfile(&phead);
read_userfile(&pheaduser);
print_log();
enter= choose_num(&pheaduser,&id,&password);
if(enter==1)
{
printf("---登录成功---\n\n\n");
print_meau();
while(1)
{
printf("请输入指令:");
scanf("%d", &N);
putchar('\n');
switch(N)
{
case 1:ReadBook(&phead); break;
case 2:print_book(phead); break;
case 3:scanf_search(inform);print_search(SearchBook(phead,inform),inform); break;
case 4:cutoff_book(&phead); break;
case 5:write_bookfile(phead);write_userfile(pheaduser);releasebook(phead);releaseuser(pheaduser);exit(1); break;
default:printf("指令错误\n"); break;
}
putchar('\n');
}
}
return 0;
}
要调试程序
人造人 发表于 2018-12-29 19:23
要调试程序
什么意思,我不会调试,是不是哪里有错误啊
费曼 发表于 2018-12-29 19:32
什么意思,我不会调试,是不是哪里有错误啊
要学习调试,程序代码量超过几百行后,恐怕没有人能够保证不会出错,出错了就要调试,调试很重要
从图中可以看到read_userfile函数没有按照预期工作
人造人 发表于 2018-12-29 19:45
要学习调试,程序代码量超过几百行后,恐怕没有人能够保证不会出错,出错了就要调试,调试很重要
从图 ...
那你调试后要怎么修改呢,我现在不会,但我想知道怎么改
费曼 发表于 2018-12-29 19:46
那你调试后要怎么修改呢,我现在不会,但我想知道怎么改
抱歉,我选择退出,让其他人帮你吧
刚刚的回复怎么没有了 第一个错误 read_userfile函数里,while循环判断条件应该用判断文件结尾函数,而不是读取字符函数,应该改为 !feof(fp)
第二个错误 read_userfile函数里,第二个fscanf里字符串的 密码 前面加一个空格 fscanf(fp, " 密码:%u\n", &user->password);
第三个错误 write_userfile函数里,count变量每次循环都赋值为1,应该把count定义在while循环外面
第四个错误 read_bookfile函数里,while循环判断条件应该用判断文件结尾函数,而不是读取字符函数,应该改为 !feof(fp) rencaixiaomeng 发表于 2018-12-29 22:36
第一个错误 read_userfile函数里,while循环判断条件应该用判断文件结尾函数,而不是读取字符函数,应该改 ...
照着你的改了,但还是不行 rencaixiaomeng 发表于 2018-12-29 22:36
第一个错误 read_userfile函数里,while循环判断条件应该用判断文件结尾函数,而不是读取字符函数,应该改 ...
我懂了,但是为什么注册两次,多出来一个
文件里的信息是这样的
--用户信息--
账号:2密码:2
--用户信息--
账号:1密码:1
--用户信息--
账号:4063608密码:4063608
rencaixiaomeng 发表于 2018-12-29 22:36
第一个错误 read_userfile函数里,while循环判断条件应该用判断文件结尾函数,而不是读取字符函数,应该改 ...
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char title;
char author;
float price;
struct Date date;
char publisher;
struct Book*pnext;
};
struct USER
{
unsigned int id;
unsigned int password;
struct USER *pnext;
};
void read_bookfile(struct Book**pphead)
{
FILE*fp;
if((fp=fopen("图书信息.txt","r"))==NULL)
{
if((fp=fopen("图书信息.txt","w"))==NULL)
{
printf("打开文件失败!\n");
exit(EXIT_FAILURE);
}
fclose(fp);
if((fp=fopen("图书信息.txt","r"))==NULL)
{
printf("打开文件失败!\n");
exit(EXIT_FAILURE);
}
}
while(!feof(fp))
{ struct Book *book=(struct Book*)malloc(sizeof(struct Book));
fscanf(fp,"书名:%s\n",book->title);
fscanf(fp,"作者:%s\n",book->author);
fscanf(fp,"售价:%f\n",&book->price);
fscanf(fp,"出版日期:%d-%d-%d\n",&book->date.year,&book->date.month,&book->date.day);
fscanf(fp,"出版社:%s\n\n",book->publisher);
book->pnext=*pphead;
*pphead=book;
}
fclose(fp);
}
void write_bookfile(struct Book*book)
{
FILE*fp;
if((fp=fopen("图书信息.txt","w"))==NULL)
{
printf("打开文件失败!\n");
exit(EXIT_FAILURE);
}
while(book!=NULL)
{
fprintf(fp,"书名:%s\n",book->title);
fprintf(fp,"作者:%s\n",book->author);
fprintf(fp,"售价:%.2f\n",book->price);
fprintf(fp,"出版日期:%d-%d-%d\n",book->date.year,book->date.month,book->date.day);
fprintf(fp,"出版社:%s\n\n",book->publisher);
book=book->pnext;
}
fclose(fp);
}
void read_userfile(struct USER**pphead)
{
FILE*fp;
if((fp=fopen("用户信息.txt","r"))==NULL)
{
if((fp=fopen("用户信息.txt","w"))==NULL)
{
printf("打开文件失败!\n");
exit(EXIT_FAILURE);
}
fclose(fp);
if((fp=fopen("用户信息.txt","r"))==NULL)
{
printf("文件打开失败!\n");
exit(EXIT_FAILURE);
}
}
while(!feof(fp))
{
struct USER *user=(struct USER*)malloc(sizeof(struct USER));
fscanf(fp,"--用户信息--\n 账号:%u",&user->id);
fscanf(fp,"密码:%u\n",&user->password);
user->pnext=*pphead;
*pphead=user;
}
fclose(fp);
}
void write_userfile(struct USER*user)
{
FILE*fp;
if((fp=fopen("用户信息.txt","w"))==NULL)
{
printf("打开文件失败!\n");
exit(EXIT_FAILURE);
}
while(user!=NULL)
{
fprintf(fp,"--用户信息--\n 账号:%u",user->id);
fprintf(fp,"密码:%u\n",user->password);
user=user->pnext;
}
fclose(fp);
}
void print_log(void)
{
printf("\n\n----------------欢迎来到图书管理系统----------------------\n\n\n");
printf(" 1 登录\n");
printf(" 2 注册\n");
printf(" 3 退出\n\n\n");
printf(" ---请输入操作前的序号进行相应操作---\n\n");
}
void AddUser(struct USER**pphead)
{
struct USER*newuser=(struct USER*)malloc(sizeof(struct USER));
if(newuser==NULL)
{
printf("分配内存失败!\n");
exit(EXIT_FAILURE);
}
printf("--注册新账户--\n");
printf("账户:");
scanf("%u",&newuser->id);
printf("密码:");
scanf("%u",&newuser->password);
newuser->pnext=*pphead;
*pphead=newuser;
putchar('\n');
}
void log_on(unsigned int*id,unsigned int*password)
{
printf("——请输入账号和密码(未注册请先注册账户)——\n");
printf("账号:");
scanf("%u",id);
printf("密码:");
scanf("%u",password);
putchar('\n');
}
int confirm_user(struct USER*user,unsigned int id,unsigned int password)
{
while(1)
{
if(user==NULL) //要放前面
{
return -1;
}
if(id==user->id&&password==user->password)
{
return 1;
}
user=user->pnext;
}
}
int choose_num(struct USER**pphead,unsigned int *id,unsigned int *password)
{
int key=-1,kiss;
do{
printf("输入序号:");
scanf("%d",&kiss);
putchar('\n');
switch(kiss)
{
case 1:log_on(id,password);key=confirm_user(*pphead,*id,*password);
if(key!=1)
{
printf("账号或密码错误!\n\n");
}
break; //+break
case 2:AddUser(pphead);break;
case 3:write_userfile(*pphead);exit(1);break;
default:printf("输入指令错误!\n");
}
}while(key!=1);
return key;
}
void print_meau(void)
{
putchar('\n');
printf("%46c图书管理系统\n", ' ');
printf("\n\n");
printf("* * * * * * * * * * * * * * * * * * *\n");
printf("*1 录入图书 *\n");
printf("*2 打印已录入图书信息 *\n");
printf("*3 搜素图书 *\n");
printf("*4 删除已录入图书 *\n");
printf("*5 退出程序 *\n");
printf("* * * * * * * * * * * * * * * * * * *\n");
printf(" \n\n ---输入功能前的相应序号进行操作---\n\n");
}
void ReadBook(struct Book**pphead)
{
struct Book*book = (struct Book*)malloc(sizeof(struct Book));
printf("---开始录入书籍---\n");
getchar();
printf("请输入书名:");
gets(book->title);
printf("请输入作者:");
gets(book->author);
printf("请输入售价:");
scanf("%f", &book->price);
printf("请输入出版日期(按“ 2018-12-12 ”的格式输入):");
scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
getchar();//重要
printf("请输入出版社:");
//scanf("%s",book->publisher);
gets(book->publisher);
if(*pphead == NULL)
{
*pphead = book;
book->pnext = NULL;
}
else
{
struct Book*temp = *pphead;
while(1)
{
if(temp->pnext == NULL)
{
break;
}
temp = temp->pnext;
}
temp->pnext = book;
book->pnext = NULL;
}
}
void print_book(struct Book*phead)
{
struct Book *book = phead;
printf("\n---开始打印图书信息---\n\n");
if(phead == NULL)
{
printf("还未录入图书!\n");
}
else
{
while(1)
{
if(book == NULL)
{
break;
}
else
{
printf("书名:%s\n", book->title);
printf("作者:%s\n", book->author);
printf("售价:%.2f", book->price);
printf("出版日期:%d-%d-%d\n", book->date.year, book->date.month, book->date.day);
printf("出版社:%s\n", book->publisher);
putchar('\n');
book = book->pnext;
}
}
}
printf("\n---打印完毕---\n");
}
void scanf_search(char inform[])
{
printf("请输入搜索图书的的书名或作者:");
getchar(); //没有的话会异常退出
gets(inform);
}
struct Book*SearchBook(struct Book*book,char inform[])//搜索图书并返回指向该图书信息的指针,未找到则返回NULL
{
while(1)
{
if(book == NULL)
{
return book;
}
if(!strcmp(book->title, inform) || !strcmp(book->author, inform))
{
return book;
}
book = book->pnext;
}
}
void print_search(struct Book*book,char inform[]) //打印被搜索的图书信息
{
if(book == NULL)
{
printf("未找到图书信息!\n");
}
else
{
putchar('\n');
printf("---已找到被搜索图书,图书信息如下---\n\n");
do{
printf("书名:%s\n",book->title);
printf("作者:%s\n",book->author);
printf("售价:%.2f\n",book->price);
printf("出版日期:%d-%d-%d\n",book->date.year,book->date.month,book->date.day);
printf("出版社:%s\n",book->publisher);
putchar('\n');
}while((book=SearchBook(book->pnext,inform))!=NULL);
}
}
void cutoff_book(struct Book**pphead)//删除被搜索的图书信息 ,并释放内存
{
char cutbook;
struct Book *pervious = NULL;
struct Book *current = *pphead;
printf("请输入你要删除的图书的书名或作者:");
getchar();
gets(cutbook);
/* if(current == NULL)
{
printf("未找到需要删除的图书\n");
return; //返回
}*/
while(1)
{
if(current == NULL)
{
break;
}
if(!strcmp(current->title, cutbook) || !strcmp(current->author, cutbook))
{
break;
}
pervious = current;
current = current->pnext;
}
if(current == NULL)
{
printf("未找到需要删除的图书!\n");
}
else
{
if(pervious==NULL)
{
*pphead=current->pnext;
}
else
{
pervious->pnext=current->pnext;
}
free(current);
}
}
void releasebook(struct Book*book)//释放链表空间
{
struct Book *temp = NULL;
while(book != NULL)
{
temp = book;
book = book->pnext;
free(temp);
}
}
void releaseuser(struct USER*user)
{
struct USER *temp = NULL;
while(user!= NULL)
{
temp = user;
user = user->pnext;
free(temp);
}
}
int main(void)
{
int N,enter=-1;
char inform;
struct Book*phead = NULL;
struct USER *pheaduser=NULL;
unsigned int id,password;
read_bookfile(&phead);
read_userfile(&pheaduser);
print_log();
enter= choose_num(&pheaduser,&id,&password);
if(enter==1)
{
printf("---登录成功---\n\n\n");
print_meau();
while(1)
{
printf("请输入指令:");
scanf("%d", &N);
putchar('\n');
switch(N)
{
case 1:ReadBook(&phead); break;
case 2:print_book(phead); break;
case 3:scanf_search(inform);print_search(SearchBook(phead,inform),inform); break;
case 4:cutoff_book(&phead); break;
case 5:write_bookfile(phead);write_userfile(pheaduser);releasebook(phead);releaseuser(pheaduser);exit(1); break;
default:printf("指令错误\n"); break;
}
putchar('\n');
}
}
return 0;
}
rencaixiaomeng 发表于 2018-12-29 22:36
第一个错误 read_userfile函数里,while循环判断条件应该用判断文件结尾函数,而不是读取字符函数,应该改 ...
图书信息的文件也有这中情况
书名:x>
作者:
售价:0.00
出版日期:0-0-0
出版社:
书名:1
作者:1
售价:1.00
出版日期:2-2-2
出版社:1
rencaixiaomeng 发表于 2018-12-29 22:36
第一个错误 read_userfile函数里,while循环判断条件应该用判断文件结尾函数,而不是读取字符函数,应该改 ...
好像是用feof()导致多读取的一次 你在读取文件的前,加上一个ftell(fp)判断文件长度
页:
[1]