Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.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
重命名和删除文件不会';t仅在第一次迭代中工作(C)_C_File_Binaryfiles - Fatal编程技术网

重命名和删除文件不会';t仅在第一次迭代中工作(C)

重命名和删除文件不会';t仅在第一次迭代中工作(C),c,file,binaryfiles,C,File,Binaryfiles,我创建了一个程序,创建一个二进制文件,插入员工的数据(代码、姓名、薪水)。 然后程序通过用户输入增加每个员工的工资,并检查工资是否高于阈值。如果是,将从文件中删除该员工 一切都近乎完美,但有一个问题。。我针对两种情况提出了解决方案: 如果没有文件,创建一个,然后更新它-100%工作 否则(如果已经存在文件),询问用户是否要编辑该文件。(如果他选择“否”,则程序将创建新文件)。在进入更新部分时,由于某些原因,这两个选项中的任何一个都不起作用,并且没有进行任何更改。。程序打印旧文件。 但是,如果我关

我创建了一个程序,创建一个二进制文件,插入员工的数据(代码、姓名、薪水)。 然后程序通过用户输入增加每个员工的工资,并检查工资是否高于阈值。如果是,将从文件中删除该员工

一切都近乎完美,但有一个问题。。我针对两种情况提出了解决方案:

如果没有文件,创建一个,然后更新它-100%工作

否则(如果已经存在文件),询问用户是否要编辑该文件。(如果他选择“否”,则程序将创建新文件)。在进入更新部分时,由于某些原因,这两个选项中的任何一个都不起作用,并且没有进行任何更改。。程序打印旧文件。 但是,如果我关闭窗口并再次运行该程序,它将第二次运行

我可以看到程序创建的temp.dat文件在第一次迭代时仍然存在

有什么建议吗

主要内容:

更新功能:

void updateSalary(char* filename, float threshold)
{
    employee temp;
    char c;
    float increase;
    FILE *fup = fopen(filename, "rb+");
    FILE *ftmp = fopen("temp.dat", "wb+");
    if (fup == NULL)
    {
        printf("Unable to read the file\n");
        return;
    }
    while (fread(&temp, sizeof(employee), 1, fup)) //fread will return 0 when reaching EOF - safer then using (!feof)
    {
        printf("How big is the increase to %s's salary?\n", temp.name);
        scanf("%f", &increase);
        temp.salary += increase;

        if (temp.salary<threshold)
        {
            fwrite(&temp, sizeof(employee), 1, ftmp);
        }
    }
    fclose(ftmp);
    fclose(fup);

remove(filename);
rename("temp.dat", filename);`enter code here`
}
void updateSalary(字符*文件名,浮点阈值)
{
员工临时工;
字符c;
浮动增加;
FILE*fup=fopen(文件名为“rb+”);
文件*ftmp=fopen(“温度数据”、“wb+”);
如果(fup==NULL)
{
printf(“无法读取文件\n”);
返回;
}
while(fread(&temp,sizeof(employee),1,fup))//fread在达到EOF时将返回0-比使用(!feof)更安全
{
printf(“增加%s的工资有多大?\n”,临时名称);
scanf(“%f”,增加(&R);
临时工资+=增加;

如果(temp.salary在编写代码时,我假设如果读取文件的尝试失败(
if(fin==NULL)
),那么文件的指针(
fin
)也会“关闭”(就像fclose();那样),我可以轻松地编写一个新文件

我对fopen();和fclose();函数的了解还不够,但似乎,如果要检查是否有成功的尝试,读取文件,然后再写入文件,则必须使用fclose();在读取文件之前或之后,要么成功,要么不成功

更新的主要内容:

void Ex1()
{
    char filename[13] = "employee.dat";
    char c=NULL;
    float threshold = 5000;
    FILE *fin = fopen(filename, "rb");

    if (fin == NULL)
    {
        printf("Creating a new file.. \n");
        fclose(fin);
        fin = fopen(filename, "wb"); //creating a new blank file
        fclose(fin);
        get_Employee(filename);
        printf("Okay, this is what we have:\n");
        print_Bin_File(filename);
        printf("Now, let's increase the salary!\n");
        updateSalary(filename, threshold);
        printf("The updated file:\n");
        print_Bin_File(filename);
    }
    else
    {
        while (c != 'Y' || c != 'y' || c != 'N' || c != 'n')
        {
            printf("There is already an existing file:\n");
            print_Bin_File(filename);
            printf("Do you wish to update the current file? (Y/N)\n");
            rewind(stdin);
            c = getchar();
            rewind(stdin);
            if (c == 'Y' || c == 'y')
            {
                fclose(fin);
                printf("Ok, let's increase the salary!\n");
                updateSalary(filename, threshold);
                print_Bin_File(filename);
                return;
            }
            else
            {
                if (c == 'N' || c == 'n')
                {
                    printf("Creating a new file.. \n");
                    fclose(fin);
                    fin = fopen(filename, "wb"); //creating a new blank file
                    fclose(fin);
                    get_Employee(filename);
                    printf("Okay, this is what we have:\n");
                    print_Bin_File(filename);
                    printf("Now, let's increase the salary!\n");
                    updateSalary(filename, threshold);
                    print_Bin_File(filename);
                    return;
                }
                else
                    printf("invalid input\n");

            }
        }
    }

}

在执行
fin=fopen(文件名,“wb”)之前,请尝试
fclose(fin);
如果文件存在,但用户希望截断它。@4386427刚刚开始工作!感谢Sofek或@4386427,请回答或删除该问题,以便将其从未回答的问题列表中删除。@Yunnosch我不想回答,因为我无法确切解释当你
fopen
带有wb的文件时会发生什么虽然该文件已经用rb打开。这样做对我来说似乎不好,但…我不确定。因此我的评论主要是猜测。如果您愿意,请随时提供答案。您需要检查所有系统函数调用的所有返回值。如果您在Windows上,并且您尝试这样做
file*fin=fopen(filename,“rb”);fin=fopen(文件名,“wb”);
第二次
fopen
将失败,您将永远不会知道这一点,并花费数小时调试您的程序。
void Ex1()
{
    char filename[13] = "employee.dat";
    char c=NULL;
    float threshold = 5000;
    FILE *fin = fopen(filename, "rb");

    if (fin == NULL)
    {
        printf("Creating a new file.. \n");
        fclose(fin);
        fin = fopen(filename, "wb"); //creating a new blank file
        fclose(fin);
        get_Employee(filename);
        printf("Okay, this is what we have:\n");
        print_Bin_File(filename);
        printf("Now, let's increase the salary!\n");
        updateSalary(filename, threshold);
        printf("The updated file:\n");
        print_Bin_File(filename);
    }
    else
    {
        while (c != 'Y' || c != 'y' || c != 'N' || c != 'n')
        {
            printf("There is already an existing file:\n");
            print_Bin_File(filename);
            printf("Do you wish to update the current file? (Y/N)\n");
            rewind(stdin);
            c = getchar();
            rewind(stdin);
            if (c == 'Y' || c == 'y')
            {
                fclose(fin);
                printf("Ok, let's increase the salary!\n");
                updateSalary(filename, threshold);
                print_Bin_File(filename);
                return;
            }
            else
            {
                if (c == 'N' || c == 'n')
                {
                    printf("Creating a new file.. \n");
                    fclose(fin);
                    fin = fopen(filename, "wb"); //creating a new blank file
                    fclose(fin);
                    get_Employee(filename);
                    printf("Okay, this is what we have:\n");
                    print_Bin_File(filename);
                    printf("Now, let's increase the salary!\n");
                    updateSalary(filename, threshold);
                    print_Bin_File(filename);
                    return;
                }
                else
                    printf("invalid input\n");

            }
        }
    }

}