qq2874015291
qq2874015291
2021-01-17 15:29

c语言图书馆管理系统中的加入书籍模块,单独测试正常但放入主函数中引用无法进行输入

  • c语言
#include<stdio.h>

void backfunc(void);
void addbook(void);
void removebook(void);
void searchbook(void);
void listbook();
char str[20];

     int a,c,d;
     char b;
FILE *fptr;

char b;

struct library{
    
     int a,c,d;
     char b;
};
int main(){
    system("cls");
    int s;

printf("             library management system menu                     \n");
puts("1.add book");
puts("2.remove book");
puts("3.search book");
puts("4.display all the books");
printf("choose a number:");
scanf("%d",&s);
switch(s){
 case 1:
      addbook();break;
 case 2:
      removebook();break;
 case 3:
      searchbook();break;
 case 4:
      listbook();break;
 default:
      system("cls");
      printf("type number please.");
      backfunc();break;
      }
}




void backfunc(void){
    printf("\n press enter back to menu");
    b:
     if(getch()==13)
        main();
     else
        goto b;
     }
void addbook(void){
    system("cls");

FILE *fptr;
    fptr=fopen("D://newfile.txt","a+");
    char str[20];
    printf("add a book id and the name:");
    fgets(str, 19, stdin);
    fputs(str, fptr);
    backfunc();
    }
removebook(){
    int c;
FILE *frea ,*fwri;
frea=fopen("D://newfile.txt","r");
fwri=fopen("D://newfile2.txt","w");

    system("cls");
     printf("\nenter the book num that you want to remove");
     scanf("%d",&c);
     if(frea==NULL||fwri==NULL){
    printf("something is wrong");
}while(fgets(str,20,frea))
      if(str[0]!=c)
          fputs(str,fwri);
      fclose(frea);
      fclose(fwri);
      remove("newfile.txt");
      rename("newfile2.txt","newfile.txt");


     printf("\n now it is removed");
     backfunc();}
void searchbook(void){

    system("cls");
    printf("\ntype the numb of the book");
    int i;
    FILE *fp1;

    int CurrentIndex=0;
    char StrLine[1000];
    printf("type the book id ");
    scanf("%d",&i);
    if((fp1 = fopen("D://newfile.txt","r")) == NULL)
    {
        printf("error!");
        return NULL;
    }

    while (!feof(fp1))
    {

        if (CurrentIndex==i)
        {
            fgets(StrLine,1000,fp1);
            printf("%s", StrLine);
            return StrLine;
        }
        fgets(StrLine,1000,fp1);
        CurrentIndex++;

    }
    fclose(fp1);
    backfunc();}//why this back is useless?
void listbook(){
    system("cls");
   FILE *fptr;
   char c;
   fptr=fopen("D://newfile.txt","r");
   while(1)
{
    c=fgetc(fptr);
    if(c==EOF)
        break;
    else printf("%c",c);
}
fclose(fptr);
 backfunc();}
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

0条回答