代码中的函数问题及其一些问题

时间:2019-05-08 19:07:08

标签: c

Picture此程序必须执行菜单选项。首先,它以必须保存用户数据文件的功能开始,但是在执行代码后会发生错误。如何更改程序,使其首先将数据写入文件,然后再读取。 searchbyfamname和searchbybooks应该寻找书籍和家庭的读者。是否存在写入错误,如果是,该如何更改代码?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct catalogue
{
char code [8];
char name [80];
char address [80];
char famname [20];
char fathname [20];
char EGN [10];
int numbooks;
struct catalogue*next;

};
struct catalogue* head=NULL;
struct catalogue*current=NULL;
void start();
void reg();
void searchbybooks();
void searchbyfamname();
void del();
void ShowAll();
void End();
int main()
{


char code [8],name [80],address[80],famname[20],fathname[20],EGN [10];
int j,m,n,k,i,e;
int flag=-1;
FILE *fp;
fp=fopen ("catalogue.txt","wb");

printf ("Enter the new ID:\n");
scanf("%s",code);
j=strlen (code);
j++;
printf ("Enter the new name:\n");
scanf ("%s",name);
m=strlen(name);m++;
printf ("Enter a new father name:\n");
scanf ("%s",fathname);
i=strlen(fathname);i++;
printf ("Enter a new family name:\n");
scanf("%s",famname);
k=strlen(famname);k++;

printf ("Enter the new address:\n");
scanf ("%s",address);
n=strlen (address);n++;
printf ("Enter the new EGN:\n");
scanf ("%s",EGN);
e=strlen (EGN);e++;



fwrite(&j,sizeof(int),1,fp);
fwrite(code,j,1,fp);
fwrite(&m,sizeof(int),1,fp);
fwrite(name,m,1,fp);
fwrite(&k,sizeof(int),1,fp);
fwrite(famname,k,1,fp);
fwrite(&i,sizeof(int),1,fp);
fwrite(fathname,i,1,fp);
fwrite(&n,sizeof(int),1,fp);
fwrite(address,n,1,fp);
fwrite(&e,sizeof(int),1,fp);
fwrite(EGN,e,1,fp);
fwrite(&flag,sizeof(int),1,fp);
fclose(fp);





  int choice=0;

  for (;;)
  {
   printf ("----------------MENU----------------\n");
   printf ("0.Show everything saved in the file.\n");
   printf ("1.Register new reader.\n");
   printf ("2.Search readers with most books.\n");
   printf ("3.Search reader by family name.\n");
   printf ("4.Delete a reader by code number only if no books are 
   taken.\n");
   printf ("5.Exit.\n");

   do {
    printf ("Enter your choice:\n");
    scanf ("%d",&choice);
    fflush (stdin);
   }
    while(!(choice>=0 && choice<=5));
    printf("%d\n",choice);
    switch(choice)
    {
            case 0:
    {
        start();
        ShowAll();
        head=NULL;
        break;

    }
            case 1:
    {
        start();
        reg();
        End();
        head=NULL;
        break;
    }
            case 2:
    {   start();
        searchbybooks();
        head=NULL;
        break;
    }
            case 3:
    {   start();
        searchbyfamname();
        head=NULL;
        break;
    }
            case 4:
    {   start();
        del();
        End ();
        head=NULL;
        break;
    }
            case 5:
    {
        exit(5);
    }
    }
}
return 0;
}
void start ()
{
FILE *fp;
int i;
int flag;
if (fp=fopen("catalogue","rb")==NULL)
{
    printf ("Error opening file for reading.\n");
    exit (1);
}

while(1){
    if(fread(&flag, sizeof(int), 1, fp)!=1)
        {
            printf("Error reading!");

            exit(4);
        }
        if (flag==-1)
        break;

        struct catalogue*temp=(struct catalogue*)malloc(sizeof(struct 
        catalogue));
        if(head==NULL)
        {
            head=temp  ;
            current=head;
            current->next=NULL;
        }
        else
        {
        current->next=temp;
        current=temp;
        current->next=NULL;
        }
        if((fread(temp->code,flag,1,fp))!=1){
            printf("Error!");
            exit (20);
            }

        if((fread(temp->name,flag,1,fp))!=1){
            printf("Error!");
            exit (20);
            }
        if((fread(temp->address,flag,1,fp))!=1){
            printf("Error!");
            exit (20);
            }
        if((fread(temp->EGN,flag,1,fp))!=1){
            printf("Error!");
            exit (21);
            }
        if((fread(&temp->numbooks,sizeof(int),1,fp)!=1)){
            printf("Error!");
            exit(23);
        }
      }
     fclose(fp);
     }
     void reg ()
    {
  struct catalogue *temp;
temp=head;
while(temp->next!=NULL)
{
    temp=temp->next;
}
struct catalogue *newcat;
newcat=(struct catalogue*) malloc(sizeof(struct catalogue));
printf("Enter the new ID:\n ");
scanf("%s",newcat->code);
printf("Enter the new name:\n ");
scanf("%s",&newcat->name);
printf("Enter the new father name:\n ");
scanf("%s",&newcat->fathname);
printf("Enter the new family name:\n ");
scanf("%s",&newcat->famname);
printf("Enter the new address:\n ");
scanf("%s",&newcat->address);
printf("Enter the new EGN:\n ");
scanf("%s",&newcat->EGN);
newcat->next=NULL;
temp->next=newcat;
printf("Add new client:SUCCESSFUL!\n");
}
void searchbybooks()
{
char *arr;
int flag=0;
 arr=(char*)malloc(20);
printf("Input client taken books: \n \n");
gets(arr);
struct catalogue*temp;
temp=(struct catalogue*)malloc(sizeof(struct catalogue));
 temp=head;
while(temp!=NULL)
{
 if(!(strcmp(temp->numbooks,arr))){
printf("\t %d \n \n",temp->numbooks);
flag=1;
break;
}
 temp=temp->next;
}
if(flag==0) {
printf("Can't find client with %d taken books! \n \n",arr);
 }

 }

 void searchbyfamname()
 {
 char *arr;
 int flag=0;
 arr=(char*)malloc(20);
 printf("Input client family name: %s  \n \n");
 gets(arr);
struct catalogue*temp;
temp=(struct catalogue*)malloc(sizeof(struct catalogue));
temp=head;
while(temp!=NULL)
{
 if(!(strcmp(temp->famname,arr))){
printf("\t %s \n \n",temp->famname);
flag=1;
break;
}
temp=temp->next;
 }
if(flag==0) {
 printf("Can't find client with %s family name. - %s \n \n",arr);
 }

 }
 void del()
 {
 struct catalogue* prev_item=head;
struct catalogue* curr_item=head;
int f=0;
char *arr;
arr=(char*)malloc(20);
printf("Input client ID: \n");
gets(arr);
while(curr_item!=NULL)
{
       if(strcmp (curr_item->code, arr) == 0)
       {
        if(curr_item == head)
        {
            head = head->next;
            prev_item = head;
            f=1;
        }
        else
        prev_item->next = curr_item->next;
        free(curr_item);
        curr_item = prev_item;
    }
    prev_item = curr_item;
    if(f == 0)
        curr_item=curr_item->next;
    f=0;
    }
    }
    void ShowAll()
    {
   struct catalogue*m;
   for(m=head; m!=NULL; m=m->next)
   {
            printf("ID: %s  name: %s %s %s  address: %s   EGN: %s \n \n", m- 
    >code, m->name,m->fathname,m->famname , m->address, m->EGN);
    }
    }
    void End ()
    {
    FILE *fp;
    int flag=-1;
    if((fp=fopen("catalogue","wb"))==NULL){
    printf("Error opening file!");
    exit(0);
    }
    struct catalogue *temp;
    temp=head;
    int j,k,m,i,n,e;
    while(temp!=NULL){
    j=strlen(temp->code);j++;
    if(fwrite(&j,sizeof(int),1,fp)!=1){
       printf("Error!");
       exit(1);
    }
   if(fwrite(temp->code,j,1,fp)!=1){
        printf("Error!");
        exit(1);
   }
   k=strlen(temp->name);k++;
   if(fwrite(&k,sizeof(int),1,fp)!=1){
       printf("Error!");
       exit(1);
    }
    if(fwrite(temp->name,k,1,fp)!=1){
        printf("Error!");
        exit(1);
   }
    i=strlen(temp->fathname);i++;
    if(fwrite(&i,sizeof(int),1,fp)!=1){
       printf("Error!");
       exit(1);
    }
      if(fwrite(temp->fathname,i,1,fp)!=1){
        printf("Error!");
        exit(1);
   }
    n=strlen(temp->famname);n++;
       if(fwrite(&n,sizeof(int),1,fp)!=1){
       printf("Error!");
       exit(1);
    }
      if(fwrite(temp->famname,n,1,fp)!=1){
        printf("Error!");
        exit(1);
   }

   m=strlen(temp->address);m++;
    if(fwrite(&m,sizeof(int),1,fp)!=1){
       printf("Error!");
       exit(1);
    }
   if(fwrite(temp->address,m,1,fp)!=1){
        printf("Error!");
        exit(1);
   }
   e=strlen(temp->EGN);e++;
      if(fwrite(&e,sizeof(int),1,fp)!=1){
       printf("Error!");
       exit(1);


   if(fwrite(temp->EGN,e,1,fp)!=1){
        printf("Error!");
        exit(1);
   }
   if(fwrite(&temp->numbooks,sizeof(int),1,fp)!=1){
        printf("Error!");
        exit(1);
   }
    temp=temp->next;
    }
   if(fwrite(&flag,sizeof(int),1,fp)!=1){
        printf("Error!");
       exit(1);
    }
     fclose(fp);
    }
     return 0;
     }

0 个答案:

没有答案