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;
}