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: \n");
        printf("1. Add New Book\n");
        printf("2. Display Books\n");
        printf("3. Edit Book\n");
        printf("4. Delete Book\n");
        printf("5. Exit\n");
        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(" %[^\t\n]s",b.title);

    printf("Enter Author:");
    scanf(" %[^\t\n]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("\n===========================================\n");
      printf("%-5s\t%-25s  %s\n","Id","Title","Author");
      printf("\n-------------------------------------------\n");

      while(fread(&b,sizeof(Book),1,fp)==1){
        printf("%d\t%-25s %s\n",b.id,b.title,b.author);
      }
      printf("\n===========================================\n");
      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("\n-----------------\nBook to be edited\n-------------------\n Id: %d\n Title: %s\n Author: %s\n-----------------\n",b.id,b.title,b.author);

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

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


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

           }

    }


}

Comments 0


Share