该结构是否会用一个声明保存多个条目的信息?

So, my task is to write a program, which reads information about books in a library, and it cannot save information for more than one book. I have declared typedef structures for the books and the library. The pointer inside the library structure is a dynamic array, whose elements point to the book structure. Any suggestions?

typedef struct Sbooks
{
    char book_name[32];
    char book_genre[32];
    char author[32];
    int page_count;
    float price;

}books;
typedef struct Slibrary
{
    char library_name[32];
    struct Sbooks * bookp;
}lib;
void addBook(lib library,books book);
int main()
{
        FILE *fp;
        int i=1;
        lib library;
        books book;
        library.bookp = (books*)calloc(i,sizeof(books));
        fp=fopen("Books.bin","rb");
        if(fp==NULL)
        {
            printf("Error. File not found.");
            exit(1);
        }

        if(i>1)
        {
            library.bookp = (books*)realloc(library.bookp,sizeof(book)*i);
        }
        fread(library.bookp->book_name,sizeof(char),32,fp);
        fread(library.bookp->book_genre,sizeof(char),32,fp);
        fread(library.bookp->author,sizeof(char),32,fp);
        fread(&(library.bookp->page_count),sizeof(int),1,fp);
        fread(&(library.bookp->price),sizeof(float),1,fp);
        i++;
    }
    fclose(fp);
if(strcmp(Choice,"add")==0)
    {
        addBook(library, book);
    }
return 0;
}
void addBook(lib library,books book)
{
    printf("Enter the name of the book: ");
    scanf("%s\n", library.bookp->book_name);
    printf("Enter the genre of the book: ");
    scanf("%s\n", library.bookp->book_genre);
    printf("Enter the author of the book: ");
    scanf("%s\n", library.bookp->author);
    printf("Enter the page count of the book: ");
    scanf("%d\n", &(library.bookp->page_count));
    printf("Enter the price of the book: ");
    scanf("%f\n", &(library.bookp->price));

}

评论