#include <stdio.h>
#include <stdlib.h>
struct lib
{
char num[20];
char name[20];
char writer[20];
char pub[20];
};
typedef struct lib L;
L book[200],books;
FILE *fp;
void add();
void list();
void search();
int main()
{
int n;
while(1)
{
system("cls");
printf("Welcome!\n");
printf("1.list. \n2.add book.\n");
printf("3.search book.\n4.delete book.\n");
printf("5.exit.\n ");
printf("Please input number:\n");
scanf("%d",&n);
if(n==1)
list();
else if(n==2)
add();
else if(n==3)
search();
// else if(n==4)
// del();
else if(n==5)
exit(0);
else
{
printf("wrong number!\n");
system("pause");
}
return 0;
}
void search()
{
int i=0;
int m=0;
char number[20];
printf("Please input number to search :");
scanf("%s",number);
if ((fp=fopen ("12.txt","r"))==NULL)
{
printf("File open error!\n");
exit (0);
}
while(!feof(fp))
{
fscanf(fp,"%s %s %s %s ",book[i].num, book[i].name, book[i].writer, book[i].pub);
if(book[i].num==number)
{
printf("------------------------------------------\n");
printf("| number | book name | author | pulish |\n");
printf("| %4s |%4s |%5s |%4s |\n",book[i].num, book[i].name, book[i].writer, book[i].pub);
printf("------------------------------------------\n");
m=1;
break;
}
i++;
}
if(m==0)
printf("No this book.");
fclose(fp);
system("pause");
}
void list()
{
int i=0;
if ((fp=fopen ("12.txt","r"))==NULL)
{
printf("File open error!\n");
exit (0);
}
printf("------------------------------------------\n");
printf("| number | book name | author | pulish |\n");
while(!feof(fp))
{
fscanf(fp,"%s %s %s %s ",book[i].num, book[i].name, book[i].writer, book[i].pub);
printf("------------------------------------------\n");
printf("| %4s |%4s |%5s |%4s |\n",book[i].num, book[i].name, book[i].writer, book[i].pub);
i++;
}
fclose(fp);
printf("------------------------------------------\n");
system("pause");
}
void add()
{ system("cls");
if ((fp=fopen ("12.txt","a"))==NULL)
{
printf("File open error!\n");
exit (0);
}
printf("Please input number:");
scanf("%s",books.num);
printf("Please input book name:");
scanf("%s",books.name);
printf("Please input author:");
scanf("%s",books.writer);
printf("Please input publish:");
scanf("%s",books.pub);
fprintf(fp,"%s\ %s\ %s\ %s\ ",books.num, books.name, books.writer, books.pub);
fclose(fp);
}
添加 search函数之后(51) : error C2143: syntax error : missing ';' before 'type' 出现bao'cuo