C 使用文件删除学生数据库程序中不起作用的函数
它创建包含所需结果的临时文件。但它不会删除以前的文件records.txt,也不会重命名temp.txt。IM使用DEV C++。p>C 使用文件删除学生数据库程序中不起作用的函数,c,database,file,C,Database,File,它创建包含所需结果的临时文件。但它不会删除以前的文件records.txt,也不会重命名temp.txt。IM使用DEV C++。p> void delete() { FILE *fp, *ft; int no; char ch; student stud; fp = fopen("Records.txt","r"); printf("\nEnter student's RollNo to delete:"); scanf("%d",&
void delete()
{
FILE *fp, *ft;
int no;
char ch;
student stud;
fp = fopen("Records.txt","r");
printf("\nEnter student's RollNo to delete:");
scanf("%d",&no);
ft = fopen("temp.txt","w+");
while (!feof(fp))
{
fscanf(fp, "\n%d\t%s\t", &stud.roll_no, stud.name);
if (stud.roll_no != no)
{
fprintf(ft, "\n\t%d \t%s", stud.roll_no, stud.name);
}
}
fclose(fp);
fclose(ft);
remove("Records.txt");
rename("temp.txt","Records.txt");
}
第一个问题。你绝对不会做错误检查。检查每一个可能的错误,你就会知道为什么事情不起作用,例如删除错误返回-1,设置errno,这样你就可以使用strerror并检查实际不起作用的内容。看起来像是学校作业。。。还请使用调试器,以便逐行检查代码。您当前的代码也有一定的风险,在最坏的情况下,您可能会丢失records.txt文件。谢谢。是的,我会照你说的做@iharob要挑剔的是,如果这些函数失败,它们将返回一个非零值。所以不要检查ifremove…==-1,但类似int failed=remove。。。;如果失败{。。。