从二进制文件删除

斯蒂芬·福克斯

我试图通过在Employee.Firstname的第一个字节上用'*'标记二进制文件来删除Employee,但是如果文件上有多个雇员,我尝试通过标记来删除第二个雇员用“ *”删除它时,只会用“ *”标记第一个Employee.Firstname的第一个字节。

这是我写入文件的方式:

struct EmployeeInformation{

        char Firstname[32];
        char Lastname[32];
        char Address[32];
        char ID[8];
        char Duration[8];
    }

struct EmployeeInformation Employee;

void AddEmployee()
{
    FILE *fd;

    printf("\n\n>>Add Employee<<\n");

    //I use fgets and get all Employee info I just didn't include it for reading purposes.

    if((fd = fopen(BINARY_FILE, "ab+")) == NULL)
    {
        printf("Error in opening file.\n\n");
        getchar();
    }


    else
    {
        //Write Employee to file.
        fwrite(&Employee, sizeof(Employee), 1, fd);

        printf("\nEmployee Added!\n\n");
    }
    fclose(fd);


}

这是删除员工的代码。

void DeleteEmployee()
{
    FILE *fd;
    char EmployeeID[8];
    long Pos = 0;
    fpos_t pos;

    printf("\n>>Delete Employee<<\n");

    //Ask user for ID of employee they wish to delete.
    printf("Employee ID:");
    fgets(EmployeeID, 6, stdin);

    if ((fd = fopen(BINARY_FILE, "rb+")) == NULL)
    {

        printf("Error, Cannot Open File.\n");
    }
    else
    {

        while(fread(&Employee, sizeof(struct EmployeeInformation), 1, fd) != 0)
        {

            if(strcmp(EmployeeID, Employee.ID) == 0)
            {
                    //Employee Found
                    printf("Employee Found!\n");

                    //Get Current Position in file.
                    Pos = fgetpos(fd, &pos);

                    //Go to that Postion.
                    fseek(fd, Pos, SEEK_SET);
                    //Mark for deletion with '*'
                    fputc('*', fd);

                    printf("Firstname: %s\n",Employee.Firstname);
            }
            else
            {
                printf("Employe Not Found!\n\n");
            }
        }
    }
    fclose(fd);

}
凯里·格雷戈里(Carey Gregory)

您所获得的位置fgetpos您刚刚阅读的记录之后您需要fgetpos在进行读取sizeof(struct EmployeeInformation)之前先调用,或者在之前从该位置减去fseek当您向后搜索然后写入一个字节时,还会弄乱当前位置。最后,fgetpos不返回您认为的结果;替换为ftell

我建议这样做:

long int pos = ftell(fd);
while(fread(&Employee, sizeof(struct EmployeeInformation), 1, fd) != 0)
{
    if(strcmp(EmployeeID, Employee.ID) == 0)
    {
            //Employee Found
            printf("Employee Found!\n");

            //Go to the BEGINNING of the record
            fseek(fd, pos, SEEK_SET);
            //Mark for deletion with '*'
            fputc('*', fd);

            printf("Firstname: %s\n",Employee.Firstname);

            //Now position back to the end of the record (-1 because we advanced 1
            //byte when we did fputc() above
            fseek(pd, pos+sizeof(struct EmployeeInformation)-1, SEEK_SET);
    }
    else
    {
        printf("Employe Not Found!\n\n");
    }

    //We should now be at the beginning of the next record - mark the position
    pos = ftell(fd);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章