简体   繁体   English

从C中的Bin文件中删除记录

[英]Deleting a record from a Bin File in C

I'm trying to delete a record from a bin file.我正在尝试从 bin 文件中删除一条记录。

I tried to make another pointer and make a temp file to write the data into it and then from the temp file write the data back to the original file..I suppose there is maybe an easier way.我试图制作另一个指针并制作一个临时文件以将数据写入其中,然后从临时文件将数据写回原始文件..我想可能有更简单的方法。 but the main question is what to change in update salary function.但主要问题是更新工资功能要改变什么。

here is what I did so far.这是我到目前为止所做的。

So the main goal of the program is if I enter extra money that is bigger than the threshold I should remove the record.所以该计划的主要目标是,如果我输入的额外资金大于阈值,我应该删除记录。 any suggestions what do I need to change in update salary function?有什么建议我需要在更新工资功能中改变什么?

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>

    typedef struct employee
    {
        int  code;
        char  name[15];
        float  salary;
    } Employee;

    void create_bin(char *f, float* threshold);
    void updateSalary(char* filename, float threshold);
    void Display(char *fName);

    void main() {
        char filename[20] = "input.bin";
        float threshold;
        create_bin(filename,&threshold);
        Display(filename);
        updateSalary(filename, threshold);
        Display(filename);
        getch();
    }

    void create_bin(char *f,float* threshold){
        FILE *f_b;
        Employee emp1;
        int object=0,number,i=0;
        float amount;
        f_b = fopen(f, "wb+");
        if (!f_b) {
            printf("unable to open file");
        }
        printf("How many Employees?");
        scanf("%d",&number);
        for (i = 0; i < number; i++) {
            printf("Eneter employee Code:");
            scanf("%d",&emp1.code);
            rewind(stdin);
            puts("Enter name");
            gets(emp1.name);
            printf("Eneter employee Salary:");
            scanf("%f",&emp1.salary);
            rewind(stdin);
            object += fwrite(&emp1, sizeof(Employee), 1, f_b);
        }
        printf("Ente threshold:");
        scanf("%.2f",&amount);
        *threshold = amount;
        printf("Total elements in file %d\n", object);
        fclose(f_b);
    }

    void updateSalary(char* filename, float threshold) 
    {
        float extra_money;
        int i = 1;
        Employee emp;
        FILE *f = fopen(filename,"rb+");
        FILE *f_temp = fopen("Final_file","wb+");
        if (!f)
        {
            printf("File not found!\n");
            return 0;
        }
        if (!f_temp) {
            printf("File not found!\n");
            return 0;
        }
        fread(&emp, sizeof(Employee), 1, f);
        while (!feof(f))
        {   
            printf("Enter how much money to add to #%d worker:",i++);
            rewind(stdin);
            scanf("%f",&extra_money);
                emp.salary+= extra_money;
                if (emp.salary <= threshold) {
                    fwrite(&emp, sizeof(Employee), 1, f_temp);
                }
                fread(&emp, sizeof(Employee), 1, f);
        }
        fread(&emp, sizeof(Employee), 1, f_temp);
        while (!feof(f_temp)) {
            fwrite(&emp, sizeof(Employee), 1,f);
            fread(&emp, sizeof(Employee), 1, f_temp);
        }
        fclose(f_temp);
        fclose(f);
    }
    void Display(char *fName)
    {
        Employee emp;
        FILE *f = fopen(fName, "rb");
        if (f)
        {
            fread(&emp, sizeof(emp), 1, f);
            while (!feof(f))
            {
                printf("%9d %15s %8.2f\n", emp.code, emp.name, emp.salary);
                fread(&emp, sizeof(emp), 1, f);
            }
            fclose(f);
        }
    }

the main question is what to change in update salary function.主要问题是更新工资功能要改变什么。

Follow Scheff's good advice: Rename the new file to name of original file.遵循 Scheff 的好建议:将新文件重命名为原始文件的名称。 To do so, change为此,请更改

        fread(&emp, sizeof(Employee), 1, f_temp);
        while (!feof(f_temp)) {
            fwrite(&emp, sizeof(Employee), 1,f);
            fread(&emp, sizeof(Employee), 1, f_temp);
        }
        fclose(f_temp);
        fclose(f);

to

        fclose(f_temp);
        fclose(f);
        rename(filename, "Backup_file");    // optional, or maybe remove(filename);
        rename("Final_file", filename);

Besides that,除此之外,

        scanf("%.2f",&amount);

has an invalid conversion specification;具有无效的转换规范; perhaps you meant也许你的意思是

        scanf("%2f", &amount);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM