Home  • Programming • C

Read, Write, Edit, Delete operations using struct from and to a file in C language

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

void addBook();
void displayBook();
void deleteBook(int id);
void editBook(int id);

typedef struct{
  int id;
  char title[50];
  char author[50];
}Book;

int main()
{
    int option=-1;
     int id;
    do{
        //system("cls");
        printf("Choose your option: 
");
        printf("1. Add New Book
");
        printf("2. Display Books
");
        printf("3. Edit Book
");
        printf("4. Delete Book
");
        printf("5. Exit
");
        scanf("%d",&option);

        switch(option){
          case 1:
              addBook();
            break;
          case 2:
            displayBook();
            break;
          case 3:
            printf("Enter book id to edit:");
            scanf("%d",&id);
            editBook(id);
            break;
          case 4:
           printf("Enter book id to delete:");
            scanf("%d",&id);
            deleteBook(id);
            break;
          default:
           printf("Unknown Option");
            break;

        }

    }while(option!=5);


    return 0;
}


void addBook(){

    Book b;
    FILE *fp = fopen("Books.DAT","ab+");
    if(fp == NULL){
       puts("Cannot open file");
    }else{

    system("cls");

    printf("Enter Id:");
    scanf("%d",&b.id);

    printf("Enter Title:");
    scanf(" %[^	
]s",b.title);

    printf("Enter Author:");
    scanf(" %[^	
]s",b.author);
    //fseek(fp, sizeof(Book),SEEK_END);
    fwrite(&b,sizeof(Book),1,fp);
    fclose(fp);
    }

}

displayBook(){

    Book b;
    FILE *fp = fopen("Books.DAT","rb+");
    if(fp == NULL){
       puts("Cannot open file");
       exit(1);
    }else{

      system("cls");
      printf("
===========================================
");
      printf("%-5s	%-25s  %s
","Id","Title","Author");
      printf("
-------------------------------------------
");

      while(fread(&b,sizeof(Book),1,fp)==1){
        printf("%d	%-25s %s
",b.id,b.title,b.author);
      }
      printf("
===========================================
");
      fclose(fp);
    }


}

void deleteBook(int id){
    Book b;
    FILE *fpr = fopen("Books.DAT","rb");
    FILE *fpw = fopen("tmp.DAT","wb");

    while(fread(&b,sizeof(Book),1,fpr)==1){

           if(id!=b.id){

             fwrite(&b,sizeof(Book),1,fpw);

           }

    }

    fclose(fpw);
    fclose(fpr);

    remove("Books.DAT");
    rename("tmp.DAT","Books.DAT");
}


void editBook(int id){
    Book b;
    FILE *fp = fopen("Books.DAT","rb+");

    while(fread(&b,sizeof(Book),1,fp)==1){

           if(id==b.id){

              printf("
-----------------
Book to be edited
-------------------
 Id: %d
 Title: %s
 Author: %s
-----------------
",b.id,b.title,b.author);

              printf("Enter New Title:");
              scanf(" %[^	
]s",b.title);

              printf("Enter New Author:");
              scanf(" %[^	
]s",b.author);


             fseek(fp,ftell(fp)-sizeof(Book),0);
             fwrite(&b,sizeof(Book),1,fp);
             fclose(fp);

           }

    }


}

Comments 0


Copyright © 2024. Powered by Intellect Software Ltd