I am trying to write a program that can append a line to a text file(with input taken from the actual program) and that can delete a line with a certain word in it. When I either try to append the line at the end or simply delete a line it works as intended. The problem occurs when I add then delete a line (not necessarily the same line). If I do both operations, then the line that was just added is also deleted even if its not the line which the program meant to delete. Here is my code for the two operations:
void add(char *file, *char info){
FILE *p = fopen(file, "wt"); // check for error
fprintf(file, "%s\n", info);// close file;}
For delete, we only want to delete a single instance (first) of name
void delete(char *file, *char name){
FILE *old = fopen(file, "rt");
FILE *new = fopen("temp", "wt");
char line[1000];
fgets(line,999,old);
int deleted = 0;
while(!feof(csv_old)){
if(strstr(line, name)==0 || deleted >= 1)
fputs(line, new);
else if(strstr(line,name)!=0)
deleted++;
fgets(line,999,old);}
// close files
For example, if i have say:
this
that
this
Say I call add("filename", "those") then it appends "those" properly to the end of file (in the new file). but if I call add then immediately call delete on say "this", i get:
that
this
in the new file
I am completely stuck as to why this is. Any help would be greatly appreciated