【问题标题】:loop keep printing trash values, handeling binary files循环保持打印垃圾值,处理二进制文件
【发布时间】:2020-09-13 15:37:16
【问题描述】:

我试图找出导致这个循环处理我的结构然后继续创建垃圾值的原因,内部循环正在工作并更新薪水,而我之前作为独立使用的外部循环(如果我只使用 print使用相同 while 循环而不通过此函数的函数,它将打印我的 2 个没有垃圾值的结构)并且它有效:

while (fread(&worker, sizeof(Employee), 1, file1) == 1)
    {
        fseek(file2, 0, SEEK_SET);
        while (fread(&salary, sizeof(SalaryIncrement), 1, file2) == 1)
        {
            if (worker.id == salary.idEmployee)
            {
                worker.salary += salary.Salaryincrement;
                fseek(file1, -ByteEmpolyee, SEEK_CUR);
                fwrite(&worker, ByteEmpolyee, 1, file1);
            }
        }

    }

如果有人想看,我也会添加我的完整代码,提前致谢

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

typedef struct
{
    long id;
    char name[20];
    float salary;
}Employee;

typedef struct
{
    long idEmployee;
    float Salaryincrement;
}SalaryIncrement;

void AddingEmployee(char* fileName);
void AddingSalaryIncrement(char* filename);
void UpdateSalary(char* filename, char* filename1);
void printEmployee(char* filename);

void main()
{
    AddingEmployee("ListEmployees.bin");
    AddingSalaryIncrement("SalaryIncrement.bin");
    UpdateSalary("ListEmployees.bin", "SalaryIncrement.bin");
    printEmployee("ListEmployees.bin");
    system("pause");
}

void AddingEmployee(char* filename)
{
    FILE* file1 = fopen(filename, "ab");
    if (file1 == NULL) {
        printf("Unable to open file.\n");
        exit(1);
    }
    Employee worker;
    printf("Adding a new employee:\n");
    printf("\nPlease enter the employee's ID , name, Salary: ");
    scanf("%ld %s %f", &worker.id, worker.name, &worker.salary);
    fwrite(&worker, sizeof(Employee), 1, file1);
    fclose(file1);
}

void AddingSalaryIncrement(char* filename)
{
    FILE* file1 = fopen(filename, "ab");
    if (file1 == NULL) {
        printf("Unable to open file.\n");
        exit(1);
    }
    SalaryIncrement salary;
    printf("Add SlaryIncrement:\n");
    printf("Please inset the following: ID , salaryincrement: ");
    scanf("%ld %f", &salary.idEmployee, &salary.Salaryincrement);
    fwrite(&salary, sizeof(SalaryIncrement), 1, file1);
    fclose(file1);
}

void UpdateSalary(char* filename, char* filename1)
{
    Employee worker;
    SalaryIncrement salary;
    FILE* file1 = fopen(filename, "rb+");
    if (file1 == NULL) {
        printf("Unable to open file.\n");
        exit(1);
    }
    FILE* file2 = fopen(filename1, "rb");
    if (file2 == NULL) {
        printf("Unable to open file.\n");
        exit(1);
    }
    int ByteEmpolyee = sizeof(Employee);
    int ByteSalary = sizeof(SalaryIncrement);
    while (fread(&worker, sizeof(Employee), 1, file1) == 1)
    {
        fseek(file2, 0, SEEK_SET);
        while (fread(&salary, sizeof(SalaryIncrement), 1, file2) == 1)
        {
            if (worker.id == salary.idEmployee)
            {
                worker.salary += salary.Salaryincrement;
                fseek(file1, -ByteEmpolyee, SEEK_CUR);
                fwrite(&worker, ByteEmpolyee, 1, file1);
            }
        }

    }
    fclose(file1);
    fclose(file2);
}

void printEmployee(char* filename)
{
    Employee worker;
    FILE* file1 = fopen(filename, "rb");
    if (file1 == NULL) {
        printf("Unable to open file.\n");
        exit(1);
    }
    while (fread(&worker, sizeof(Employee), 1, file1) == 1)
    {
        printf("ID: %ld\t\t Name: %s\t\t Salary: %0.2f\n", worker.id, worker.name, worker.salary);
    }

}

【问题讨论】:

    标签: c file while-loop binaryfiles


    【解决方案1】:

    当我用这个替换它时,我能够解决这个循环:(但它仍然发生在另一个循环中,我仍然不知道为什么)

        fseek(file1, 0, SEEK_END);
        int size = ftell(file1)/sizeof(Employee);
        fseek(file1, 0, SEEK_SET);
        for (int i = 0; i < size; i++)
        {
            fread(&worker, sizeof(Employee), 1, file1);
            fseek(file2, 0, SEEK_SET);
            while (fread(&salary, sizeof(SalaryIncrement), 1, file2) == 1)
            {
                if (worker.id == salary.idEmployee)
                {
                    worker.salary += salary.Salaryincrement;
                    fseek(file1, -ByteEmpolyee, SEEK_CUR);
                    fwrite(&worker, ByteEmpolyee, 1, file1);
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多