我试图弄清楚如何从我的简单数据库中删除特定行。 运行此结果只会使Origin文件为空而不是一行 经过无尽的失败后,我现在寻求帮助。
我的代码。
void Remove_name(FILE *Array, FILE *PointTemp) {
char Tname[40]; /// string to store name
long int recsize; /// size of each record
recsize = sizeof(A);
Array = fopen ( "Orig.txt", "r" );
printf("\nEnter name to delete: ");
scanf("%s", Tname);
PointTemp = fopen("TEMP.txt","w"); /// create a intermediate file for temporary storage
rewind(Array); /// move record to starting of file
while(fread(&A,recsize,1,Array) == 1){ /// read all records from file
if(strcmp(A.firstname, Tname) != 0){ /// if the entered record match
fwrite(&A,recsize,1,PointTemp); /// move all records except the one that is to be deleted to temp file
}
}
fclose(PointTemp);
fclose(Array);
remove("Orig.txt"); /// remove the orginal file
rename("TEMP.txt", "Orig.txt"); /// rename the temp file to original file name
}
我曾经尝试过,但最终也彻底失败了。 将发布预先尝试的代码。
void Remove_name(FILE Array[Size], FILE Temp[Size]) {
char temp;
char delete_string[Size];
Array = fopen ("Orig.txt", "r" ); //Open real file for READ
temp = getc(Array);
while (temp != EOF){
printf ("%c", temp);
temp = getc(Array);
}
printf("\nEnter name to delete: ");
Temp = fopen("Taco.txt","w"); /// create a intermediate file for temporary storage
//temp = getc(Array);
scanf("%s", delete_string);
//temp = getc(Array);
while (temp != EOF) {
temp = getc(Array);
if(strcmp(A.firstname,delete_string) != 0){ /// if the entered record match
putc(A.firstname,Temp); /// move all records except the one that is to be deleted to temp file
}
}
fclose(Array);
fclose(Temp);
remove("Orig.txt"); /// remove the orginal file
rename("Taco.txt","Orig.txt"); /// rename the temp file to original file name
printf("\nThe contents of file after being modified are as follows\n");
Array = fopen("Orig.txt", "r");
temp = fgetc(Array);
while (temp != EOF){
printf ("%c", temp);
temp = fgetc(Array);
}
fclose(Array);