Я пытаюсь удалить имя Бен из файла CSV. Однако написанный код также удаляет все другие имена, кроме последнего. Это причина для этого?
FILE * pfile;
char line[21];
char *buffer;
char *ptr;
buffer = malloc(sizeof(char)*1000);
memset(buffer, 0, sizeof(char)*1000);
ptr = buffer;
//open the file
pfile = fopen("names.csv","r");
if(pfile != NULL){
while(!feof(pfile)){
fgets(line, 21, pfile);
if(strcmp(line, "ben\n")!= 0){
//put line in buffer
strcpy(buffer, line);
ptr += strlen(line);
}
}
fclose(pfile);
pfile = fopen("names.csv","w");
fprintf(pfile, "%s", buffer);
fclose(pfile);
}
else{
printf("could not read file\n");
}
getch();
return 0;}