How to display with sorting, updating and deleting the file in C?

advertisements

So I've been doing some coding for Homework and I found some problems with displaying, sorting, update, and even delete file. there are many codes that I found but none of them works, I can't even find the code for delete the file.. can you guys help me?

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

char id[20], year[20], title[20], name[20];
void write(char id[], char title[], char name[], char year[])
{
    FILE *fp;

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

    if(fp == NULL)
    {
        fp = fopen("book.txt", "w");
        fprintf(fp, "ID TITLE NAME YEAR\n");
        fprintf(fp, "%s\t\t%s\t\t%s\t\t%s\t\t", id, title, name, year);
    }
    else
    {
        fp = fopen("book.txt.", "a");
        fprintf(fp, "%s\t\t%s\t\t%s\t\t%s\t\t", id, title, name, year);
        exit(2);
    }

    fclose(fp);
}
void displayfile()
{
    FILE *fp;

    fp = fopen("book.txt", "r");
    if(fp == NULL)
    {
        printf("there is no data");
    }
    while(!feof(fp))
    {
        fscanf(fp, "%[^#]#%[^#]#%[^#]#%[^#]#%s", id, title, name, year);
        printf("\n%s\t\t\n%s\t\t\n%s\t\t\n%s\t\t", id, title, name, year);

    }
    fclose(fp);
}

void updatefile()
{
    FILE *fp;
    int ch;
    fp = fopen("book.txt", "r+");
    if(fp == NULL)
    {
        fprintf(stderr, "there is no data");
        exit(1);
    }
    while ((ch = fgetc(fp)) !=EOF)
    {
        if(ch == 'i')
        {
            fseek(fp, -1, SEEK_CUR);
            fputc('a', fp);
            fseek(fp, 0, SEEK_CUR);
        }
    }
    fclose(fp);
}

void deletefile()
{
    int apus;
    FILE *fp;

    fp = fopen("book.txt", "r");
}

int  main() {

int option;

    printf("please select one of this : \n");
    printf("1. Input Book Record\n");
    printf("2. Display Book Record\n");
    printf("3. Update Book Record\n");
    printf("4. Erase Book Record\n");
    scanf("%d", &option);

    switch(option)
    {
    case 1:
       printf("Book ID?");
       scanf("%s", id);
       printf("Book title?");
       scanf("%s", title);
       printf("author name?");
       scanf("%s", name);
       printf("year published?");
       scanf("%s", year);
       write(id, title, name, year);
       break;
    case 2:
        displayfile();

    break;
    case 3:
        updatefile();

   }

getchar();

return 0;
}


Looking at your code, you mean with "delete a file" to delete a record from the file. Since you use text files, this means:

  1. open the input file book.txt for reading.

  2. open a new file for writing with a temporary name, e.g. tmpbook.txt.

  3. read the input file, writing each record that must not be deleted to the temporary file.

  4. if you have read the record to be deleted, don't do anything ("skip it").

  5. read all the remaining book records and write them to the temporary file.

  6. close input and temporary file.

  7. unlink book.txt.

  8. rename tmpbook.txt to book.txt.

Now you are done.


The above procedure is for sequential files. Compare it with deleting a sentence from a text document. After the delete, all text moves up so the text reads contiguous again. Sentences do not have the same length.

For random access files (like databases), you can delete a record by giving it a marker that it has been deleted. In you program managing the file, you now simply do not show a record if the marker says it has been deleted, and if a new record is entered, you can re-use a deleted record. For this to work, all records must have the same size. You can use seek here to position the file pointer and you open the file for both reading and writing.