一个程序输出有误啊!!!!在线等!!急

altairheat 2012-03-03 08:02:14

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define L sizeof(struct book)
void submenu1();
void submenu2();
void Overview();
void Add();
void Del();
void Amend();
void SearchISBN();
void Searchname();
void Searchpress();
void Save();
void Reload();
void End();
struct book *pt;
struct book *pt2;
struct book
{
char ISBN[13];
char bookname[50];
char bookauthor[50];
char bookpress[50];
char dateofpublication[8];
float price;
struct book *next;
};
struct book *creat()
{
FILE *lf;int n=0;
struct book *head;
struct book *p1,*p2;
head=NULL;
if((lf=fopen("bookdatabase.txt","r"))==NULL)
{
printf("Fatal Error:load file failed\nProgram will exit now\n");
exit(0);
}
p1=(p2=(struct book *)malloc(L));
if(fscanf(lf,"%s\n",&p1->ISBN)==EOF){exit(0);}printf("%s",&p1->ISBN);
fscanf(lf,"%s\n",&p1->bookname);printf("%s",p1->bookname);
fscanf(lf,"%s\n",&p1->bookauthor);printf("%s",&p1->bookauthor);
fscanf(lf,"%s\n",&p1->bookpress);printf("%s",&p1->bookpress);
fscanf(lf,"%s\n",&p1->dateofpublication);printf("%s",&p1->dateofpublication);
fscanf(lf,"%f\n",&p1->price);printf("%f",&p1->price);
while(feof(lf)==0)
{
if(n==0){head=p1;n++;}
else {p2->next=p1;}
p2=p1;
p1=(struct book *)malloc(L);
fscanf(lf,"%s\n",&p1->ISBN);printf("%s",&p1->ISBN);
fscanf(lf,"%s\n",&p1->bookname);printf("%s",p1->bookname);
fscanf(lf,"%s\n",&p1->bookauthor);printf("%s",&p1->bookauthor);
fscanf(lf,"%s\n",&p1->bookpress);printf("%s",&p1->bookpress);
fscanf(lf,"%s\n",&p1->dateofpublication);printf("%s",&p1->dateofpublication);
fscanf(lf,"%f\n",&p1->price);printf("%f",&p1->price);
}
p2->next=NULL;
pt2=p2;
printf("Loaded file successfully\n");
fclose(lf);
return head;
}
int login(char password[])
{
char temp[20];int i=0;
for(i;i<20;i++){temp[i]='\0';}
printf("Please input your password\n");
gets(temp);
if(strcmp(password,temp)==0)
{
printf("Login successfully,welcome!\n");
return 1;
}
else return -1;
}
void setpassword(char password[])
{
char temp[20];int i=0;int j=0;
FILE *p;
system("cls");
if((p=fopen("pwf.txt","w"))==NULL)
{
printf("Fatal Error:load file failed\nProgram will exit now\n");
exit(0);
}
for(i;i<20;i++){temp[i]='\0';}
printf("Please input your new password\n");
getchar();
gets(temp);
fputs(temp,p);
printf("This is your new password,please remember it\n");
puts(temp);
fclose(p);
}
void menu()
{
char choose;
system("cls");
printf("1.Revise(add,del,amend)\n2.Search\n3.Overview\n4.Exit\n");
printf("Please give your next instruction\n");
getchar();
choose=getchar();
switch(choose)
{
case '1':submenu1();break;
case '2':submenu2();break;
case '3':Overview();
case '4':exit(0);
}

}
void submenu1()
{
char choose;
system("cls");
printf("1.Add\n2.Del\n3.Amend\n4.Menu\n5.Exit\n");
printf("Please give your next instruction\n");
getchar();
choose=getchar();
switch(choose)
{
case '1':Add();break;
case '2':Del();break;
case '3':Amend();break;
case '4':menu();
case '5':exit(0);
}
}
void submenu2()
{
char choose;
system("cls");
printf("1.Searh by ISBN\n2.Search by name\n3.Search press and it's book\n4.Menu\n5.Exit\n");
printf("Please give your next instruction\n");
getchar();
choose=getchar();
switch(choose)
{
case '1':SearchISBN();break;
case '2':Searchname();break;
case '3':Searchpress();break;
case '4':menu();
case '5':exit(0);
}
}
void Add()
{
struct book *p0;char yn;
system("cls");
p0=(struct book *)malloc(L);
printf("please input following this order,ending with enter:\n");
printf("ISBN:");scanf("%s",&p0->ISBN);
printf("Name of the book:");scanf("%s",&p0->bookname);
printf("Author of the book:");scanf("%s",&p0->bookauthor);
printf("Press of the book:");scanf("%s",&p0->bookpress);
printf("Date of publication(eg.20120101):");scanf("%s",&p0->dateofpublication);
printf("Price(default RMB,just input the value,eg.10.0):");scanf("%f",&p0->price);getchar();
printf("Are you really want to add this book ?If so,please input 'Y';else input'N':");
if((yn=getchar())=='Y')
{
pt2->next=p0;
p0->next=NULL;
pt2=p0;
printf("Added successfully\n");
}
End();
}
void Del()
{
struct book *p1,*p2,*p3;char yn;
system("cls");
p3=(struct book *)malloc(L);
printf("Please input the ISBN of the book you want to delete:");
scanf("%s",&p3->ISBN);
printf("Are you really want to delete this book ?If so,please input 'Y';else input'N':");
if((yn=getchar())=='Y')
{
p1=pt;
if(strcmp(p1->ISBN,p3->ISBN)==0){pt=p1->next;printf("Deleted successfully\n");}
else
{
while(p1->next!=NULL)
{
p2=p1;p1=p1->next;
if(strcmp(p1->ISBN,p3->ISBN)==0){p2->next=p1->next;printf("Deleted successfully\n");break;}
}
}
}
End();
}
void Amend()
{
struct book *p1,*p2;char yn;
system("cls");
p2=(struct book *)malloc(L);
p1=pt;
printf("Please input the ISBN of the book you want to amend:");
scanf("%s",&p2->ISBN);
printf("Are you really want to amend this book ?If so,please input 'Y';else input'N':");
if((yn=getchar())=='Y')
{
while(p1->next!=NULL)
{
if(strcmp(p1->ISBN,p2->ISBN)==0)
{
printf("Please input the new ISBN of the book:");scanf("%s",&p1->ISBN);
printf("Please input the name:");scanf("%s",&p1->bookname);
printf("Please input the author:");scanf("%s",&p1->bookauthor);
printf("Please input the press:");scanf("%s",&p1->bookpress);
printf("Please input the date of publication:");scanf("%s",&p1->dateofpublication);
printf("Please input price:");scanf("%f",&p1->price);
printf("Amended successfully\n");
break;
}
else p1=p1->next;
}
}
End();
}
void SearchISBN()
{
struct book *p1,*p2;
system("cls");
p1=pt;
p2=(struct book *)malloc(L);
printf("Please intput the ISBN you want to search:");
scanf("%s",&p2->ISBN);
while(p1->next!=NULL)
{
if(strcmp(p1->ISBN,p2->ISBN)==0)
{
printf("ISBN:%s",p1->ISBN);
printf("The name of the name:%s",p1->bookname);
printf("The author of the book:%s",p1->bookauthor);
printf("The press of the book:%s",p1->bookpress);
printf("Date of publication:%s",p1->dateofpublication);
printf("The price of the book:%.2f",p1->price);
p1=p1->next;
break;
}
else p1=p1->next;
}
End();
}
void Searchname()
{
struct book *p1,*p2;
system("cls");
p1=pt;
p2=(struct book *)malloc(L);
printf("Please intput the ISBN you want to search:");
scanf("%s",&p2->bookname);
while(p1->next!=NULL)
{
if(strcmp(p1->bookname,p2->bookname)==0)
{
printf("ISBN:%s",p1->ISBN);
printf("The name of the name:%s",p1->bookname);
printf("The author of the book:%s",p1->bookauthor);
printf("The press of the book:%s",p1->bookpress);
printf("Date of publication:%s",p1->dateofpublication);
printf("The price of the book:%.2f",p1->price);
p1=p1->next;
break;
}
else p1=p1->next;
}
End();
}
void Searchpress()
{
struct book *p1,*p2;
system("cls");
p1=pt;
p2=(struct book *)malloc(L);
printf("Please intput the ISBN you want to search:");
scanf("%s",&p2->bookpress);
while(p1->next!=NULL)
{
if(strcmp(p1->bookpress,p2->bookpress)==0)
{
printf("ISBN:%s",p1->ISBN);
printf("The name of the name:%s",p1->bookname);
printf("The author of the book:%s",p1->bookauthor);
printf("The press of the book:%s",p1->bookpress);
printf("Date of publication:%s",p1->dateofpublication);
printf("The price of the book:%.2f",p1->price);
p1=p1->next;
break;
}
else p1=p1->next;
}
End();
}
void Overview()
{
struct book *p1;
system("cls");
p1=pt;
while(p1->next!=NULL)
{
printf("ISBN:%s",p1->ISBN);
printf("The name of the name:%s",p1->bookname);
printf("The author of the book:%s",p1->bookauthor);
printf("The press of the book:%s",p1->bookpress);
printf("Date of publication:%s",p1->dateofpublication);
printf("The price of the book:%.2f\n",p1->price);
p1=p1->next;
}
End();
}
void Save()
{
FILE *sf;struct book *p1;
p1=pt;
if((sf=fopen("bookdatabase.txt","w"))==NULL)
{
printf("Fatal Error:save file failed\nProgram will exit now\n");
exit(0);
}
while(p1->next!=NULL)
{
fprintf(sf,"%s\n",p1->ISBN);
fprintf(sf,"%s\n",p1->bookname);
fprintf(sf,"%s\n",p1->bookauthor);
fprintf(sf,"%s\n",p1->bookpress);
fprintf(sf,"%s\n",p1->dateofpublication);
fprintf(sf,"%f\n",p1->price);
p1=p1->next;
}
fclose(sf);
}
void Reload()
{
creat();
}
void End()
{
printf("Back to the menu,please input '1';to exit ,please input '2':");
Here:if(getchar()=='1'){system("cls");menu();}
else
{
if(getchar()=='2'){Save();exit(0);}
else {printf("Invalid input,please input again: ");goto Here;};
}
}
main()
{
char enter;char password[20];int key=0;int i=0;
FILE *fp;
pt=creat();
if((fp=fopen("pwf.txt","r+"))==NULL)
{
printf("Fatal Error:load file failed\nProgram will exit now\n");
exit(0);
}
fgets(password,20,fp);
printf("Load file successed,please give the next instruction\n");
key=login(password);
if(key==-1)
{
printf("Fatal Error:password match failed\nProgram will exit now\n");
}
if(key==1)
{
system("cls");
printf("1.Menu\n2.Reset my password\n3.Exit\n");
printf("Please give your next instruction\n");
enter=getchar();
switch(enter)
{
case '1':menu();break;
case '2':setpassword(password);break;
case '3':exit(0);
}
}
fclose(fp);
Save();
}
...全文
211 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
duke56 2012-03-04
  • 打赏
  • 举报
回复

/*
上边的修正版吧,因为发现bookdatabase.txt是空文件的话,会出错...改了下判断条件...
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define L sizeof(struct book)

typedef struct book
{
char ISBN[13];
char bookname[50];
char bookauthor[50];
char bookpress[50];
char dateofpublication[8];
float price;
struct book *next;
}book;

book *creat()
{
FILE *lf;
book *phead = NULL,*pE = NULL,*pS;
if((lf=fopen("bookdatabase.txt","r"))==NULL)
{
printf("Fatal Error:load file failed\nProgram will exit now\n");
exit(0);
}
while (feof(lf)==0)
{
pS=(book *)malloc(L);
memset(pS,0,L);
fscanf(lf,"%s\n",&pS->ISBN);
if (strcmp(pS->ISBN,"")==0)
{
break;
}
printf("%s\n",pS->ISBN);
fscanf(lf,"%s\n",&pS->bookname);
printf("%s\n",pS->bookname);
fscanf(lf,"%s\n",&pS->bookauthor);
printf("%s\n",pS->bookauthor);
fscanf(lf,"%s\n",&pS->bookpress);
printf("%s\n",pS->bookpress);
fscanf(lf,"%s\n",&pS->dateofpublication);
printf("%s\n",pS->dateofpublication);
fscanf(lf,"%f\n",&pS->price);
printf("%f\n",pS->price);
pS->next = NULL;
if (phead==NULL)
{
phead = pE = pS;
}
else
pE->next = pS;
pE = pS;
}
printf("Loaded file successfully\n");
fclose(lf);
getch();
return phead;
}

book *Add(book *phead)
{
struct book *pS,*pE=phead;
char yn;
system("cls");
pS=(book *)malloc(L);
memset(pS,0,L);
printf("please input following this order,ending with enter:\n");
printf("ISBN:");
scanf("%s",&pS->ISBN);
printf("Name of the book:");
scanf("%s",&pS->bookname);
printf("Author of the book:");
scanf("%s",&pS->bookauthor);
printf("Press of the book:");
scanf("%s",&pS->bookpress);
printf("Date of publication(eg.20120101):");
scanf("%s",&pS->dateofpublication);
printf("Price(default RMB,just input the value,eg.10.0):");
scanf("%f",&pS->price);
getchar();
printf("Are you really want to add this book ?If so,please input 'Y';else input'N':");
if(yn=getchar(),yn=='y'||yn=='Y')
{
if (pE==NULL)
{
pE = phead = pS;
return phead;
}
else
{
while (pE->next!=NULL)
{
pE = pE->next;
}
pE->next = pS;
printf("Added successfully\n");
}
}
return phead;
}

void Save(book *phead)
{
FILE *sf;
struct book *pS = phead;
if((sf=fopen("bookdatabase.txt","w"))==NULL)
{
printf("Fatal Error:save file failed\nProgram will exit now\n");
exit(0);
}
while(pS!=NULL)
{
fprintf(sf,"%s\n",pS->ISBN);
fprintf(sf,"%s\n",pS->bookname);
fprintf(sf,"%s\n",pS->bookauthor);
fprintf(sf,"%s\n",pS->bookpress);
fprintf(sf,"%s\n",pS->dateofpublication);
fprintf(sf,"%f\n",pS->price);
pS=pS->next;
printf("save file\n");
}
fclose(sf);
}

int main()
{
book *head = creat();
head = Add(head);
Save(head);
return 0;
}
duke56 2012-03-04
  • 打赏
  • 举报
回复

/*
楼主的链表处理有问题,下边的代码是我重新帮编写的...你的代码风格真够"独一"的...
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define L sizeof(struct book)

typedef struct book
{
char ISBN[13];
char bookname[50];
char bookauthor[50];
char bookpress[50];
char dateofpublication[8];
float price;
struct book *next;
}book;

book *creat()
{
FILE *lf;
book *phead = NULL,*pE = NULL,*pS;
if((lf=fopen("bookdatabase.txt","r"))==NULL)
{
printf("Fatal Error:load file failed\nProgram will exit now\n");
exit(0);
}
while (feof(lf)==0)
{
pS=(book *)malloc(L);
memset(pS,0,L);
fscanf(lf,"%s\n",&pS->ISBN);
printf("%s\n",pS->ISBN);
fscanf(lf,"%s\n",&pS->bookname);
printf("%s\n",pS->bookname);
fscanf(lf,"%s\n",&pS->bookauthor);
printf("%s\n",pS->bookauthor);
fscanf(lf,"%s\n",&pS->bookpress);
printf("%s\n",pS->bookpress);
fscanf(lf,"%s\n",&pS->dateofpublication);
printf("%s\n",pS->dateofpublication);
fscanf(lf,"%f\n",&pS->price);
printf("%f\n",pS->price);
pS->next = NULL;
if (phead==NULL)
{
phead = pE = pS;
}
else
pE->next = pS;
pE = pS;
}
printf("Loaded file successfully\n");
fclose(lf);
getch();
return phead;
}

book *Add(book *phead)
{
struct book *pS,*pE=phead;
char yn;
system("cls");
pS=(book *)malloc(L);
memset(pS,0,L);
printf("please input following this order,ending with enter:\n");
printf("ISBN:");
scanf("%s",&pS->ISBN);
printf("Name of the book:");
scanf("%s",&pS->bookname);
printf("Author of the book:");
scanf("%s",&pS->bookauthor);
printf("Press of the book:");
scanf("%s",&pS->bookpress);
printf("Date of publication(eg.20120101):");
scanf("%s",&pS->dateofpublication);
printf("Price(default RMB,just input the value,eg.10.0):");
scanf("%f",&pS->price);
getchar();
printf("Are you really want to add this book ?If so,please input 'Y';else input'N':");
if(yn=getchar(),yn=='y'||yn=='Y')
{
if (pE==NULL)
{
pE = phead = pS;
return phead;
}
else
{
while (pE->next!=NULL)
{
pE = pE->next;
}
pE->next = pS;
printf("Added successfully\n");
}
}
return phead;
}

void Save(book *phead)
{
FILE *sf;
struct book *pS = phead;
if((sf=fopen("bookdatabase.txt","w"))==NULL)
{
printf("Fatal Error:save file failed\nProgram will exit now\n");
exit(0);
}
while(pS!=NULL)
{
fprintf(sf,"%s\n",pS->ISBN);
fprintf(sf,"%s\n",pS->bookname);
fprintf(sf,"%s\n",pS->bookauthor);
fprintf(sf,"%s\n",pS->bookpress);
fprintf(sf,"%s\n",pS->dateofpublication);
fprintf(sf,"%f\n",pS->price);
pS=pS->next;
printf("save file\n");
}
fclose(sf);
}

int main()
{
book *head = creat();
Add(head);
Save(head);
return 0;
}
altairheat 2012-03-04
  • 打赏
  • 举报
回复
真的很谢谢你,虽然我和朋友一起搞了出来,但还是要很谢谢你,你是个好人
altairheat 2012-03-03
  • 打赏
  • 举报
回复
你如果想帮我编译的话,你要复制我最上面的那个版本!!!!!!,下面的回帖的那个是给主要东西注释的!!!!!!!!!!请注意
矫情狗_____ 2012-03-03
  • 打赏
  • 举报
回复
链接出错了...
Linking...
1.obj : error LNK2001: unresolved external symbol _Searchpress
1.obj : error LNK2001: unresolved external symbol _Searchname
1.obj : error LNK2001: unresolved external symbol _SearchISBN
Debug/1.exe : fatal error LNK1120: 3 unresolved externals
执行 link.exe 时出错.

1.exe - 1 error(s), 0 warning(s)
altairheat 2012-03-03
  • 打赏
  • 举报
回复

这是我的格式:
11111111111111
a
b
c
20120101
10.0
11111111111112
a
b
c
20120101
10.0
11111111111113
a
b
c
20120101
10.0
读取是对的,根据我调试加入的printf语句,创建链表时的输出到链表里的数据都对
altairheat 2012-03-03
  • 打赏
  • 举报
回复
所以我用了\n啊,格式是每行只有一个数据项
altairheat 2012-03-03
  • 打赏
  • 举报
回复
那我马上注释,对不起:请大家主要看creat函数(创建链表)add,del,amend()修改,save这几个函数吧,

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define L sizeof(struct book)
void submenu1();
void submenu2();
void Overview();
void Add();
void Del();
void Amend();
void SearchISBN();
void Searchname();
void Searchpress();
void Save();
void Reload();
void End();
struct book *pt;
struct book *pt2;
struct book
{ //这是个结构体,放图书的信息。
char ISBN[13];
char bookname[50];
char bookauthor[50];
char bookpress[50];
char dateofpublication[8];
float price;
struct book *next;
};
struct book *creat()
{
//创建链表,我的做法是,程序中链表处理数据,离开程序是在写入文件
FILE *lf;int n=0;
struct book *head;
struct book *p1,*p2;
head=NULL;
if((lf=fopen("bookdatabase.txt","r"))==NULL)//打开一个文件
{
printf("Fatal Error:load file failed\nProgram will exit now\n");
exit(0);
}
p1=(p2=(struct book *)malloc(L));
//从文件里读取数据,后面printf语句是我调试时看的,下同
if(fscanf(lf,"%s\n",&p1->ISBN)==EOF){exit(0);}printf("%s",&p1->ISBN);
fscanf(lf,"%s\n",&p1->bookname);printf("%s",p1->bookname);
fscanf(lf,"%s\n",&p1->bookauthor);printf("%s",&p1->bookauthor);
fscanf(lf,"%s\n",&p1->bookpress);printf("%s",&p1->bookpress);
fscanf(lf,"%s\n",&p1->dateofpublication);printf("%s",&p1->dateofpublication);
fscanf(lf,"%f\n",&p1->price);printf("%f",&p1->price);
while(feof(lf)==0)
{
if(n==0){head=p1;n++;}
else {p2->next=p1;}
p2=p1;
p1=(struct book *)malloc(L);
fscanf(lf,"%s\n",&p1->ISBN);printf("%s",&p1->ISBN);
fscanf(lf,"%s\n",&p1->bookname);printf("%s",p1->bookname);
fscanf(lf,"%s\n",&p1->bookauthor);printf("%s",&p1->bookauthor);
fscanf(lf,"%s\n",&p1->bookpress);printf("%s",&p1->bookpress);
fscanf(lf,"%s\n",&p1->dateofpublication);printf("%s",&p1->dateofpublication);
fscanf(lf,"%f\n",&p1->price);printf("%f",&p1->price);
}
p2->next=NULL;
pt2=p2;
printf("Loaded file successfully\n");
fclose(lf);
return head;
}
int login(char password[])//登录验证密码,别管它
{
char temp[20];int i=0;
for(i;i<20;i++){temp[i]='\0';}
printf("Please input your password\n");
gets(temp);
if(strcmp(password,temp)==0)
{
printf("Login successfully,welcome!\n");
return 1;
}
else return -1;
}
void setpassword(char password[])
{
char temp[20];int i=0;int j=0;
FILE *p;
system("cls");
if((p=fopen("pwf.txt","w"))==NULL)
{
printf("Fatal Error:load file failed\nProgram will exit now\n");
exit(0);
}
for(i;i<20;i++){temp[i]='\0';}
printf("Please input your new password\n");
getchar();
gets(temp);
fputs(temp,p);
printf("This is your new password,please remember it\n");
puts(temp);
fclose(p);
}
//以下3个函数是菜单,可以不用理,没错误
void menu()
{
char choose;
system("cls");
printf("1.Revise(add,del,amend)\n2.Search\n3.Overview\n4.Exit\n");
printf("Please give your next instruction\n");
getchar();
choose=getchar();
switch(choose)
{
case '1':submenu1();break;
case '2':submenu2();break;
case '3':Overview();
case '4':exit(0);
}

}
void submenu1()
{
char choose;
system("cls");
printf("1.Add\n2.Del\n3.Amend\n4.Menu\n5.Exit\n");
printf("Please give your next instruction\n");
getchar();
choose=getchar();
switch(choose)
{
case '1':Add();break;
case '2':Del();break;
case '3':Amend();break;
case '4':menu();
case '5':exit(0);
}
}
void submenu2()
{
char choose;
system("cls");
printf("1.Searh by ISBN\n2.Search by name\n3.Search press and it's book\n4.Menu\n5.Exit\n");
printf("Please give your next instruction\n");
getchar();
choose=getchar();
switch(choose)
{
case '1':SearchISBN();break;
case '2':Searchname();break;
case '3':Searchpress();break;
case '4':menu();
case '5':exit(0);
}
}
//就下面这里开始有错!!!!
void Add()
{
struct book *p0;char yn;
system("cls");
p0=(struct book *)malloc(L);
printf("please input following this order,ending with enter:\n");
printf("ISBN:");scanf("%s",&p0->ISBN);
printf("Name of the book:");scanf("%s",&p0->bookname);
printf("Author of the book:");scanf("%s",&p0->bookauthor);
printf("Press of the book:");scanf("%s",&p0->bookpress);
printf("Date of publication(eg.20120101):");scanf("%s",&p0->dateofpublication);
printf("Price(default RMB,just input the value,eg.10.0):");scanf("%f",&p0->price);getchar();
printf("Are you really want to add this book ?If so,please input 'Y';else input'N':");
if((yn=getchar())=='Y')
{
pt2->next=p0;
p0->next=NULL;
pt2=p0;
printf("Added successfully\n");
}
End();
}
void Del()
{
struct book *p1,*p2,*p3;char yn;
system("cls");
p3=(struct book *)malloc(L);
printf("Please input the ISBN of the book you want to delete:");
scanf("%s",&p3->ISBN);
printf("Are you really want to delete this book ?If so,please input 'Y';else input'N':");
if((yn=getchar())=='Y')
{
p1=pt;
if(strcmp(p1->ISBN,p3->ISBN)==0){pt=p1->next;printf("Deleted successfully\n");}
else
{
while(p1->next!=NULL)
{
p2=p1;p1=p1->next;
if(strcmp(p1->ISBN,p3->ISBN)==0){p2->next=p1->next;printf("Deleted successfully\n");break;}
}
}
}
End();
}
void Amend()
{
struct book *p1,*p2;char yn;
system("cls");
p2=(struct book *)malloc(L);
p1=pt;
printf("Please input the ISBN of the book you want to amend:");
scanf("%s",&p2->ISBN);
printf("Are you really want to amend this book ?If so,please input 'Y';else input'N':");
if((yn=getchar())=='Y')
{
while(p1->next!=NULL)
{
if(strcmp(p1->ISBN,p2->ISBN)==0)
{
printf("Please input the new ISBN of the book:");scanf("%s",&p1->ISBN);
printf("Please input the name:");scanf("%s",&p1->bookname);
printf("Please input the author:");scanf("%s",&p1->bookauthor);
printf("Please input the press:");scanf("%s",&p1->bookpress);
printf("Please input the date of publication:");scanf("%s",&p1->dateofpublication);
printf("Please input price:");scanf("%f",&p1->price);
printf("Amended successfully\n");
break;
}
else p1=p1->next;
}
}
End();
}
//我把源代码的搜索函数都删除了
void Overview()
{
struct book *p1;
system("cls");
p1=pt;
while(p1->next!=NULL)
{
printf("ISBN:%s",p1->ISBN);
printf("The name of the name:%s",p1->bookname);
printf("The author of the book:%s",p1->bookauthor);
printf("The press of the book:%s",p1->bookpress);
printf("Date of publication:%s",p1->dateofpublication);
printf("The price of the book:%.2f\n",p1->price);
p1=p1->next;
}
End();
}
void Save()
{
FILE *sf;struct book *p1;
p1=pt;
if((sf=fopen("bookdatabase.txt","w"))==NULL)
{
printf("Fatal Error:save file failed\nProgram will exit now\n");
exit(0);
}
while(p1->next!=NULL)
{
fprintf(sf,"%s\n",p1->ISBN);
fprintf(sf,"%s\n",p1->bookname);
fprintf(sf,"%s\n",p1->bookauthor);
fprintf(sf,"%s\n",p1->bookpress);
fprintf(sf,"%s\n",p1->dateofpublication);
fprintf(sf,"%f\n",p1->price);
p1=p1->next;
}
fclose(sf);
}
void Reload()
{
creat();
}
//这个函数是执行完函数后的一个选择,可以退出或会回主菜单
void End()
{
printf("Back to the menu,please input '1';to exit ,please input '2':");
Here:if(getchar()=='1'){system("cls");menu();}
else
{
if(getchar()=='2'){Save();exit(0);}
else {printf("Invalid input,please input again: ");goto Here;};
}
}
main()
{
char enter;char password[20];int key=0;int i=0;
FILE *fp;
pt=creat();
if((fp=fopen("pwf.txt","r+"))==NULL)
{
printf("Fatal Error:load file failed\nProgram will exit now\n");
exit(0);
}
fgets(password,20,fp);
printf("Load file successed,please give the next instruction\n");
key=login(password);
if(key==-1)
{
printf("Fatal Error:password match failed\nProgram will exit now\n");
}
if(key==1)
{
system("cls");
printf("1.Menu\n2.Reset my password\n3.Exit\n");
printf("Please give your next instruction\n");
enter=getchar();
switch(enter)
{
case '1':menu();break;
case '2':setpassword(password);break;
case '3':exit(0);
}
}
fclose(fp);
Save();
}
duke56 2012-03-03
  • 打赏
  • 举报
回复
粗看了下代码,还是建议楼主用fwrite和fread函数来读写文件比较好...

你用格式化写入文本后,但以后读出来就不大清楚文本里边的格式长度了
duke56 2012-03-03
  • 打赏
  • 举报
回复
粗看了下代码,还是建议楼主用fwrite和fread函数来读写文件比较好...

你用格式化写入文本后,但以后读出来就不大清楚文本里边的格式长度了
mosal 2012-03-03
  • 打赏
  • 举报
回复
flush了吗
duke56 2012-03-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 flowingfirefly 的回复:]

找出你主要的疑惑之处,贴出来。不然,很少有人会有耐心把你这么长的代码看完
[/Quote]

还有就是代码连个注释都没有的,要看清整个代码的流程挺花时间的...
altairheat 2012-03-03
  • 打赏
  • 举报
回复
疑惑之处就是不知道为什么会输出数据到文件时出错!
altairheat 2012-03-03
  • 打赏
  • 举报
回复
输出的错误就是我自己回自己的帖子那里的错误,输出是最后一位数被他改成了a(其实下一项数据,也就是说:
本来向文件添加的应该是:
11111111111111
a
b
c
20120101
10.0
结果
111111111111a(下一行是什么,这里就是什么)
a
b
c
20120101
10.0
flowingfirefly 2012-03-03
  • 打赏
  • 举报
回复
找出你主要的疑惑之处,贴出来。不然,很少有人会有耐心把你这么长的代码看完
duke56 2012-03-03
  • 打赏
  • 举报
回复
代码也真够长的...

输出什么错误等都没说呢!!!!
altairheat 2012-03-03
  • 打赏
  • 举报
回复
求求大家了,这个系统我做了好久了,但是add函数,del函数和amend(修改)函数都有问题!!!!!!!:比如说,我添加的:
ISBN:1111111111111
name:a
author:b
press:c
date:20120101
price:10.0
本来向文件添加的应该是:
11111111111111
a
b
c
20120101
10.0
结果却是,111111111111a,后面多了个a,求大家帮下忙啊,我qq984408770,求大家教一下,我都调试了一天了,我在线等,不睡觉。

69,374

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧