-1

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;
}
CinCout
  • 9,486
  • 12
  • 49
  • 67
  • 2
    Consistent formatting is a good starting point. You should also read [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). And learn how to use debugger to step through your code. – Some programmer dude Dec 25 '16 at 12:36

2 Answers2

1

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.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
1

Add modified your code to make it works. I embedded some comments to explain the changes. I outlined the updatefile function it should be easy for you to implement. If you still need help comment please.

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

char id[20], year[20], title[20], name[20];

void writeTofile(char id[], char title[], char name[], char year[])
{
    FILE *fp;
    fp = fopen("book.txt", "a"); // if the file is not exist, it will create it.
                                // do not forget the \n not a new line 
    fprintf(fp, "%s\t%s\t%s\t%s\n", id, title, name, year);
    fclose(fp);
    exit(2);
}

void displayfile()
{
    FILE *fp;

    fp = fopen("book.txt", "r");
    if(fp == NULL)
    {
        fprintf(stderr, "there is no data");
        exit(1);
    } 

    while(!feof(fp))
    {
        fscanf(fp, "%s %s %s %s", id, title, name, year);
        printf("%s: %s\t%s\t%s\n", id, title, name, year);

    } 
    fclose(fp);
}

void updatefile(char d, char n)
{
    // open the book.txt for read
    // open the temp.txt for write
    // move all the char from the book.txt to temp.txt
    // up to the location where you want to change
    // Now add the new chars to the temp file
    // continue copying 
    // close the book.txt 
    // open it again for write (to delete its content)
    //copy temp to the book.txt
    // doen

}

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

// if you want to delete the file use
// remove("book.txt"), BUT 
//  if you want to empty the file use 
//(opening he file with white will delete its content )
fp = fopen("book.txt", "w"); 

}


int  main() {

int option;
char d, n;

    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);
       writeTofile(id, title, name, year);
       break;
    case 2:
        displayfile();

    break;
    case 3:
        printf("Character to be replaced: ");
        scanf("%c", &d);
        printf("A new character: ");
        scanf("%c", &n);
        updatefile(d, n);
    break;
    case 4:
        deletefile();
    break;
   }


getchar();

return 0;
}
Amjad
  • 3,110
  • 2
  • 20
  • 19