我试图找出导致此循环处理我的结构的原因,然后继续创建垃圾值,内部循环正在工作并更新薪水,而以前用作独立运行的外部循环(如果我仅使用打印功能谁使用相同的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);
}
}
当我用这个替换它时,我能够解决这个循环:(但是它仍然在另一个循环中发生,我仍然不知道为什么)
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);
}
}
}