Subscribe Us

Responsive Advertisement

Advertisement

Library management system

 #include<stdio.h>

#include<stdlib.h>

int passwordtime=3;

struct bookinfo{




            int id;

            char book_name[20];

            char author[20];

            int quantity;

            int rack;



};

struct bookinfo binfo;

FILE *file, *file2;

void heading()

{

    printf("\n\n\n\t\t\t##################  Welcome To Library Management System  ##################\n\n");



}

void addBook()

{


    system("cls");

    printf("\n\n\n\t\t\t##################  Add Book  ##################\n\n");

    printf("\t\t\tEnter Your Book Id: ");

    int bid,flag=0;

    scanf("%d",&bid);

    file=fopen("books.dat","ab+");

    rewind(file);

    while(fread(&binfo,sizeof(binfo),1,file)==1)

    {


        if(binfo.id==bid)

        {


            flag=1;

            break;

        }


    }

    if(flag==1)

    {

        printf("\t\t\tthis is existing book. Try another\n");

        fflush(stdin);

        getchar();

        addBook();

    }

    binfo.id=bid;

    printf("\t\t\tEnter Your Book Name : ");

    scanf("%s",binfo.book_name);

    printf("\t\t\tEnter Your Author Name : ");

    scanf("%s",binfo.author);

    printf("\t\t\tEnter Your Quantity : ");

    scanf("%d",&binfo.quantity);

    printf("\t\t\tEnter Rack : ");

    scanf("%d",&binfo.rack);


    fseek(file,0,SEEK_END);

    fwrite(&binfo,sizeof(binfo),1,file);

    fclose(file);

    printf("\t\t\tYour Book added Successfully\n");

    fflush(stdin);

    getchar();

    mainMenu();


}

void viewbook()

{

     system("cls");

     printf("\n\n\n\t\t\t##################  View Book  ##################\n\n");

     file=fopen("books.dat","rb");

     rewind(file);

     printf("\n\n\n\t\t\tID\t\tName\t\tAuthor\t\tQuantity\t\tRack\n\n");

     while(fread(&binfo,sizeof(binfo),1,file))

     {

         printf("\t\t\t%d\t\t",binfo.id);

         printf("%s\t\t",binfo.book_name);

         printf("%s\t\t",binfo.author);

         printf("%d\t\t",binfo.quantity);

         printf("%d\n",binfo.rack);




     }

     fflush(stdin);

     getchar();

     mainMenu();


}

void searchBook()

{

    system("cls");

    int id;

    printf("\n\n\n\t\t\t##################  Main Menu  ##################\n\n");

    printf("\t\t\tEnter Your Book Id : ");

    scanf("%d",&id);

    file=fopen("books.dat","rb");

    rewind(file);

    int flag=0;

    while(fread(&binfo,sizeof(binfo),1,file)==1)

    {

        if(binfo.id==id)

        {

            printf("\t\t\t\tBook is found\n");

            printf("\t\t\tName: %s\n",binfo.book_name);

            printf("\t\t\tAuthor Name : %s\n",binfo.author);

            printf("\t\t\tQuantity : %d\n",binfo.quantity);

            printf("\t\t\tRack : %d\n\n",binfo.rack);


            flag=1;


        }

    }


    if(flag==0)

    {

        printf("\t\t\tNot Found\n");

    }

    fflush(stdin);

    getchar();

    mainMenu();




}

void editbook()

{

    system("cls");

    printf("\n\n\n\t\t\t##################  Edit Book  ##################\n\n");

    printf("\t\t\tEnter Your Book Id  : ");


    int id;

    scanf("%d",&id);

    file=fopen("books.dat","rb+");

    rewind(file);

    int flag=0;

    while(fread(&binfo,sizeof(binfo),1,file)==1)

    {

        if(binfo.id==id)

        {


            printf("\t\t\t\tBook is found\n");

            printf("\t\t\tEnter New Name: ");

            scanf("%s",binfo.book_name);


            printf("\t\t\tAuthor New Name : ");

            scanf("%s",binfo.author);


            printf("\t\t\tEnter New Quantity : ");

            scanf("%d",&binfo.quantity);


            printf("\t\t\tNew Rack : ");

            scanf("%d",&binfo.rack);


            flag=1;

            fseek(file,ftell(file)-sizeof(binfo),0);

            fwrite(&binfo,sizeof(binfo),1,file);

            fclose(file);



        }

    }


    if(flag==0)

    {

        printf("\t\t\tNot Found\n");

    }

    printf("Booked Edited Successfully\n");

    fflush(stdin);

    getchar();

    mainMenu();



}

void deletebook() {

    system("cls");

    printf("\n\n\n\t\t\t##################  Delete Book  ##################\n\n");

    printf("\t\t\tEnter Your Book Id  : ");


    int id;

    scanf("%d", &id);



    file = fopen("books.dat", "rb+");


    if (file == NULL) {

        printf("\t\t\tError opening the file.\n");

        return;

    }


    int flag = 0;



    file2 = fopen("temp.dat", "wb");


    if (file2 == NULL) {

        printf("\t\t\tError opening temporary file.\n");

        fclose(file);

        return;

    }



    while (fread(&binfo, sizeof(binfo), 1, file) == 1) {

        if (binfo.id == id) {

            flag = 1;

            printf("\t\t\tBook Found. Deleting...\n");

        } else {


            fwrite(&binfo, sizeof(binfo), 1, file2);

        }

    }


    if (flag == 0) {

        printf("\t\t\tBook with ID %d not found.\n", id);

    } else {


        printf("\t\t\tBook Deleted Successfully.\n");

    }



    fclose(file);

    fclose(file2);



    if (flag == 1) {



          remove("books.dat");

        rename("temp.dat", "books.dat");




    } else {


        remove("temp.dat");

    }



     file=fopen("books.dat","rb");

     fclose(file);


    fflush(stdin);

    getchar();

    mainMenu();

}

void mainMenu()

{

    int choose;

    system("cls");


    printf("\n\n\n\t\t\t##################  Main Menu  ##################\n\n");

    printf("\n\t\t\t1.Add Book\n");

    printf("\n\t\t\t2.View Book List\n");

    printf("\n\t\t\t3.Search Book\n");

    printf("\n\t\t\t4.Edit Book\n");

    printf("\n\t\t\t5.Delete Book\n");

    printf("\n\t\t\t6.Help\n");

    printf("\n\t\t\t7.Exit\n");


    printf("\n\n\n\t\t\t***************************************************\n\n");

    printf("\t\t\tEnter Your Choose : ");

    scanf("%d",&choose);

    if(choose==1)

    {


        addBook();


    }

    else if(choose==2)

    {


        viewbook();


    }

    else if(choose==3)

    {


        searchBook();

    }

    else if(choose==4)

    {


        editbook();

    }

    else if(choose==5)

    {


        deletebook();

    }



}

void password()

{


    system("cls");

    heading();

    char password1[20]="password";

    char password2[20];



    printf("\t\t\tEnter Password you have %d time : ",passwordtime);

    scanf("%s",password2);

    if(strcmp(password1,password2)==0)

    {

        printf("\t\t\tLoged In Successfull\n\t\t\tPress Any key to go home\n");


    }

    else

    {

        if(passwordtime==0)

        {


            system("cls");

            printf("\t\t\t Your enter three time wrong password\n");

            fflush(stdin);

            getchar();

            exit(0);

        }

        passwordtime--;

        printf("\t\t\tWorng Password press any Key to try again\n");

        fflush(stdin);

        getchar();

        password();

    }



    fflush(stdin);

    getchar();


    mainMenu();


}

int main()

{


    password();


}


Post a Comment

0 Comments