This blog is under construction

Wednesday 31 July 2013

C program to delete a record from a file

Write a C program to delete a record from a file.


  #include <stdio.h>
  #include <string.h>
  #define MAX 256

  struct library {
        int id, bookCount;
        char name[MAX];
        float fine;
  };

  int main() {
        int id;
        FILE *fp1, *fp2;
        struct library ent;
        char filename[MAX], temp[] = "temp.txt";

        /* get the input file name from the user */
        printf("Enter your file name:");
        fgets(filename, MAX, stdin);
        filename[strlen(filename) - 1] = '\0';

        /* open the input file in read mode */
        fp1 = fopen(filename, "r");

        /* error handling */
        if (!fp1) {
                printf("Unable to open input file!!\n");
                return 0;
        }

        /* open temporary file in write mode */
        fp2 = fopen(temp, "w");

        /* error handling */
        if (!fp2) {
                printf("Unable to open temporary file!!\n");
                fclose(fp1);
                return 0;
        }

        /* print the records in the input file */
        printf("Records in input file:\n");
        while (!feof(fp1)) {
                fscanf(fp1, "%d %s %d %f\n", &ent.id, ent.name,
                                &ent.bookCount, &ent.fine);

                if (feof(fp1)) {
                        continue;
                }

                printf("Details of %s:\n", ent.name);
                printf("ID Number: %d\n", ent.id);
                printf("No of books taken: %d\n", ent.bookCount);
                printf("Fine amount: %.2f\n\n", ent.fine);
        }

        /* rewind file pointer of input file to start */
        rewind(fp1);

        /* get the record user wants to delete */
        printf("Enter the record you want to delete(ID no):");

        scanf("%d", &id);

        /* delete the given record */
        while (!feof(fp1)) {
                fscanf(fp1, "%d%s%d%f", &ent.id, ent.name,
                                &ent.bookCount, &ent.fine);

                if (feof(fp1)) {
                        continue;
                }

                if (id == ent.id)
                        continue;

                fprintf(fp2, "%d %s %d %f\n", ent.id, ent.name,
                                ent.bookCount, ent.fine);
        }

        /* close the opened files */
        fclose(fp1);
        fclose(fp2);

        /* remove the input file */
        remove(filename);

        /* rename the temporary file */
        rename(temp, filename);

        /* open the input file after delete operation */
        fp1 = fopen(filename, "r");

        /* error handling */
        if (!fp1) {
                printf("Unable to open the file %s\n", filename);
                return 0;
        }

        /* records in  input file after deletion operation */
        printf("\nRecords in input file after deletion operation:\n");
        while (!feof(fp1)) {
                fscanf(fp1, "%d %s %d %f\n", &ent.id, ent.name,
                                &ent.bookCount, &ent.fine);

                if (feof(fp1)) {
                          continue;
                }

                printf("Details of %s:\n", ent.name);
                printf("ID Number: %d\n", ent.id);
                printf("No of books taken: %d\n", ent.bookCount);
                printf("Fine amount: %.2f\n\n", ent.fine);
        }

        /* close the opened file */
        fclose(fp1);

        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your file name:library.txt
  Records in input file:
  Details of Samuel:
  ID Number: 1
  No of books taken: 4
  Fine amount: 100.74

  Details of Raja:
  ID Number: 2
  No of books taken: 3
  Fine amount: 150.11

  Details of Gopi:
  ID Number: 3
  No of books taken: 5
  Fine amount: 175.19

  Details of Sam:
  ID Number: 4
  No of books taken: 2
  Fine amount: 100.00

  Enter the record you want to delete(ID no):4

  Records in input file after deletion operation:
  Details of Samuel:
  ID Number: 1
  No of books taken: 4
  Fine amount: 100.74

  Details of Raja:
  ID Number: 2
  No of books taken: 3
  Fine amount: 150.11

  Details of Gopi:
  ID Number: 3
  No of books taken: 5
  Fine amount: 175.19






SEE ALSO

    No comments:

    Post a Comment