This is just a function in my program. I am getting error in this function.
I know it is something to do with the structs.
The error points to 'struct BOOK book;'
I am trying to print all books for user to see.
Library_Management.c: In function ‘viewbooks’:
Library_Management.c:217:14: error: storage size of ‘book’ isn’t known

Please HELP

void viewbooks()  //show the list of book persists in library

    struct BOOK;
    {
        int id[10];
        char name[20];
        char Author[20];
        int quantity;
        float Price;
        int rackno;
        char *cat;

    }

    FILE *fp;

    struct BOOK book;
    system("clear");

    printf("*********************************Book List*****************************");

    printf(" CATEGORY     ID    BOOK NAME     AUTHOR       QTY     PRICE     RackNo ");

    fp=fopen("record.txt","r");

    while(1){

        fread(&book,sizeof(book),1,fp);

        if(feof(fp))
        {
            break;
        }

        printf("%s",book.cat);

        printf("%d",book.id);

        printf("%s",book.name);

        printf("%s",book.Author);

        printf("%d",book.quantity);

        printf("%.2f",book.Price);

        printf("%d",book.rackno);

        printf("\n\n");

    }
    fclose(fp);
    //returnfunc();*/
}

Thanks for your reply deceptikon.
Your solution fixed the error but I have a new problem.

Library_Management.c:202:6: warning: conflicting types for ‘viewbooks’ [enabled by default]
Library_Management.c:38:17: note: previous implicit declaration of ‘viewbooks’ was here

Please help.

//list of header files

//#include "general_functions.h"
//#include "core_functions.h"
#include<stdio.h>

void mainmenu()
{

    int x;
    x=1;
    while(x){
        system("clear");
        printf("MAIN MENU\n");
        printf("\n 1. Add Books\n");
        printf(" 2. Delete Book\n");
        printf(" 3. Search Book\n");
        printf(" 4. View Book List\n");
        printf(" 5. Edit Book Record\n");
        printf(" 6. Change Password\n");
        printf(" 7. Close Application\n");
        x=0;

        int choice;
        printf("\nEnter your choice:");
        scanf("%d",&choice);
        switch(choice){
            case '1':
                //addbooks();
                break;
            case '2':
                //deletebooks();
                break;
            case '3':
                //searchbooks();
                break;
            case '4':
                viewbooks();
                break;
            case '5':
                //editbooks();
                break;
            case '6':
                //change_password();
                //printf("press any key to continue....");

                break;

            case '7':

                //creditNclose();
            default:

                printf("\aWrong Entry!!Please re-entered correct option");



        }
    }
}
/*void addbooks(void){    //funtion that add books
    system("cls");
    FILE *fp;
    int i, choice;
    add_window();
    gotoxy(20,22);
    printf("Enter your choice:");
    scanf("%d", &choice);
    if(choice==7)
        mainmenu() ;
    system("cls");
    fp = fopen("Record.dat","ab+");
    if(getdata(choice) == 1){
        book.cat=catagories[choice-1];
        fseek(fp,0,SEEK_END);
        fwrite(&book,sizeof(book),1,fp);
        fclose(fp);
        gotoxy(21,14);
        printf("The record is sucessfully saved");
        gotoxy(21,15);
        printf("Save any more?(Y / N):");
        if(getch()=='n')
            mainmenu();
        else{
            system("cls");
            addbooks();
        }
    }
    fclose(fp);

}

void deletebooks(){    //function that delete items from file Record.dat

    FILE *ft,*fp;

    system("cls");

    int d,findBook = 0;
    char another='y';

    while(another=='y'){  //infinite loop
        system("cls");
        gotoxy(10,5);

        printf("Enter the Book ID to  delete:");

        scanf("%d",&d);
        fp = fopen("Record.dat","rb+");

        while(fread(&book,sizeof(book),1,fp)==1){
            if(book.id==d){

                gotoxy(10,7);
                printf("The book record is available");

                gotoxy(10,8);

                printf("Book name is %s",book.name);

                gotoxy(10,9);

                printf("Rack No. is %d",book.rackno);

                findBook = 1;

                gotoxy(10,10);
                printf("Do you want to delete it?(Y/N):");

                if(getch()=='y'){
                    ft=fopen("test.dat","wb");  //temporary file for delete

                    rewind(fp);
                    while(fread(&book,sizeof(book),1,fp)==1){

                        if(book.id!=d){

                            fwrite(&book,sizeof(book),1,ft); //write all in tempory file except that

                        }                              //we want to delete
                    }

                    fclose(fp);
                    fclose(ft);

                    remove("Record.dat");

                    rename("test.dat","Record.dat"); //copy all item from temporary file to fp except that
                                        //we want to delete
                    gotoxy(10,11);

                    printf("The record is sucessfully deleted");
                }
            }
        }
        if(findBook == 0){
            gotoxy(10,10);
            printf("No record is found");
            getch();

        }
        gotoxy(10,12);
        printf("Delete another record?(Y/N)");
        fflush(stdin);
        another=getch();
    }
}



void searchbooks()
{
    system("cls");

    printf("*****************************Search Books*********************************");
    gotoxy(20,10);
    printf("\xDB\xDB\xDB\xB2 1. Search By ID");
    gotoxy(20,14);

    printf("\xDB\xDB\xDB\xB2 2. Search By Name");
    gotoxy( 15,20);
    printf("Enter Your Choice");

    switch(getch())
    {
        case '1':

            searchByID();
            break;
        break;
        case '2':
            searchByName();
            break;
        default :

            getch();
            searchbooks();
    }
}

*/
void viewbooks()  //show the list of book persists in library
{
    struct BOOK
    {
        int id;
        char name[20];
        char Author[20];
        int quantity;
        float Price;
        int rackno;
        char *cat;
    };

    FILE *fp;

    struct BOOK book;
    system("clear");

    printf("*********************************Book List*****************************");

    printf(" CATEGORY     ID    BOOK NAME     AUTHOR       QTY     PRICE     RackNo ");

    fp=fopen("record.txt","r");

    while(1){

        fread(&book,sizeof(book),1,fp);

        if(feof(fp))
        {
            break;
        }

        printf("%s",book.cat);

        printf("%d",book.id);

        printf("%s",book.name);

        printf("%s",book.Author);

        printf("%d",book.quantity);

        printf("%.2f",book.Price);

        printf("%d",book.rackno);

        printf("\n\n");

    }
    fclose(fp);
    //returnfunc();*/
}

/*
void editbooks(void)  //edit information about book

{
    system("cls");
    FILE *fp;
    int c=0,d;
    gotoxy(20,4);

    printf("****Edit Books Section****");
    char another='y';
    while(another=='y')
    {
        system("cls");

        gotoxy(15,6);
        printf("Enter Book Id to be edited:");
        scanf("%d",&d);
        fp=fopen("Record.dat","rb+");

        while(fread(&book,sizeof(book),1,fp)==1)
        {
            if(checkid(d)==0)
            {
                gotoxy(15,7);
                printf("The book is availble");
                gotoxy(15,8);
                printf("The Book ID:%d",book.id);
                gotoxy(15,9);

                printf("Enter new name:");scanf("%s",book.name);
                gotoxy(15,10);
                printf("Enter new Author:");scanf("%s",book.Author);
                gotoxy(15,11);
                printf("Enter new quantity:");scanf("%d",&book.quantity);
                gotoxy(15,12);

                printf("Enter new price:");scanf("%f",&book.Price);
                gotoxy(15,13);
                printf("Enter new rackno:");scanf("%d",&book.rackno);
                gotoxy(15,14);

                printf("The record is modified");
                fseek(fp,ftell(fp)-sizeof(book),0);

                fwrite(&book,sizeof(book),1,fp);
                fclose(fp);

                c=1;

            }

            if(c==0)

            {

                gotoxy(15,9);
                printf("No record found");
            }
        }

        gotoxy(15,16);
        printf("Modify another Record?(Y/N)");
        fflush(stdin);

        another=getch() ;
    }
        returnfunc();
}*/

int main(){
    FILE  *login;
    login = fopen("password.txt","r");
    if(login==NULL){
        printf("Database Do not exits. Be an adminstrator. Sign Up\n");
        //adminsignup();
    }else{
        //adminsignin();
    }
    mainmenu();
    return 0;
}

Functions must be declared before they can be used (function prototypes) -- did you declare viewbooks() in a header file or near the beginning of the file in which it is called?

Thanks I have fixed the error by moving the function into a header file.
Is this function correct?
The text file 'record.txt' has hello inside it.
When running this function it doesnt display any data from the text file. Is the format of data in the text file wrong or is it the actual function itself?

void viewbooks()  //show the list of book persists in library
{
    struct BOOK
    {
        int id;
        char name[20];
        char Author[20];
        int quantity;
        float Price;
        int rackno;
        char *cat;
    };

    FILE *fp;

    struct BOOK book;
    system("clear");

    printf("*********************************Book List*****************************");

    printf(" CATEGORY     ID    BOOK NAME     AUTHOR       QTY     PRICE     RackNo ");

    fp=fopen("record.txt","r");

    while(1){

        fread(&book,sizeof(book),1,fp);

        if(feof(fp))
        {
            break;
        }

        printf("%s",book.cat);

        printf("%d",book.id);

        printf("%s",book.name);

        printf("%s",book.Author);

        printf("%d",book.quantity);

        printf("%.2f",book.Price);

        printf("%d",book.rackno);

        printf("\n\n");

    }
    fclose(fp);
    //returnfunc();*/
}

You are not supposed to move the entire function into the header file -- just put the function prototype in the header file, leave the actual function in the *.c file.

Why do you have the structure inside the function? Is no other function in your program using it?

Structures that contain pointers can not be easily written or fread from data files because only the address contained in the pointer is read/written, not the string or other object to which it points. Look at the contents of the data file with Notepad or some other text editor and you will see the problem. It would simplify the problem if you change 'cat' to a character array instead of a pointer, similar to the way name and Author are declared.

Edited 3 Years Ago by Ancient Dragon

I think 'cat' is an array. Its the categories of books.
So the fread is correct. How does the data in the text file need to be formated do it can be read and displayed in the program.

I am getting output but it is looking like this.
'hihihhihi' is the contents of the record file where i'm reading from.
anyone help?

*********************************BookList*****************************
 ID      BOOK NAME       AUTHOR         QTY       PRICE       RackNo
 1751673192ihihhih
 �t�
-1078228072-0.000

zain@bobs-laptop:~/zainlms$ 

Edited 3 Years Ago by Zain_2

 book.cat=catagories[choice-1];
        fseek(fp,0,SEEK_END);
        fwrite(&book,sizeof(book),1,fp);

As I said before, the above won't work because all that is getting written to the file is the address of cagegories[chilce-1], not the string. An easier way to do it is to save the value of choice instead of the string, then the structure cat would be a simple integer instead of a pointer.

 struct BOOK;
    {
        int id[10];
        char name[20];
        char Author[20];
        int quantity;
        float Price;
        int rackno;
        int cat; // <<<<<<<<<<<<<<<<<<

    }

Now, when you want to print the string just printcategories[book.cat-1]

Edited 3 Years Ago by Ancient Dragon

hi,

i deleted the catergories as it was just a confusion. after removing that i am still getting the same output. i also include the fseek function but it still outputs the same thing. i am using linux, could there be a issue here?

fwrite() writes out the data in binary form, the same as it's represented in memory, so the integers will not be readable.

The printf() statements in viewbooks() you posted do not put any spaces between the fields, so it will all look run together on the screen.

Edited 3 Years Ago by Ancient Dragon

This article has been dead for over six months. Start a new discussion instead.