Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 使用文件删除学生数据库程序中不起作用的函数_C_Database_File - Fatal编程技术网

C 使用文件删除学生数据库程序中不起作用的函数

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",&

它创建包含所需结果的临时文件。但它不会删除以前的文件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",&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。。。;如果失败{。。。