微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C语言中的文件读取无法与联合体一起使用结构

如何解决C语言中的文件读取无法与联合体一起使用结构

我在使用 char ch 而不是 char ch [2] 时遇到困难。该代码会不断捕获其他字母或字符,而不是预期的H和S。我正在尝试读取一个employees.txt文件,其中包含以下数据: H 1111 Jane Doe 8.50 40.0 。 H =每小时雇员,1111 = ID,Jane Doe =全名,8.5 =工资率,40 =工作小时数。开头的H表示我要使用 char ch 尝试抓取的EmployeeType,但始终提供抓取不同字符的功能,而不是单独提供H和S。这是代码

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

#define ID_SIZE 4
#define NAME_SIZE 15

typedef enum {Hourly,Sales} EmployeeType;

// Employee structure with union for employee types
typedef struct {
   EmployeeType type;
   char id[ID_SIZE + 1],first_name[NAME_SIZE + 1],last_name[NAME_SIZE + 1];
   union {
      struct { float pay_rate,hours_worked; } hourly_emp;
      struct { float commission_rate,sales_amount; } sales_emp;
   };
} Employee;

// Reads the data of an employee from a file.
Employee read_data(FILE *in_file) {
    Employee emp;
    char ch;
    fscanf(in_file,"%c %s %s %s",ch,emp.id,emp.first_name,emp.last_name);

    if(ch == 'H'){
        emp.type = Hourly;
        fscanf(in_file,"%f %f",&emp.hourly_emp.pay_rate,&emp.hourly_emp.hours_worked);
    }
    else{
        emp.type = Sales;
        fscanf(in_file,&emp.sales_emp.commission_rate,&emp.sales_emp.sales_amount);
    }

    return emp;
}

float weekly_salary(Employee emp) {
    switch(emp.type){
        case Hourly: return emp.hourly_emp.pay_rate * 40 + ((emp.hourly_emp.hours_worked - 40) * emp.hourly_emp.pay_rate * 1.5);
        case Sales: return emp.sales_emp.commission_rate * emp.sales_emp.sales_amount;
        default: return -1.0;
    }
}

// Writes the data of a student and the evaluation into file.
void write_data(FILE *out_file,Employee emp,float salary) {
    fprintf(out_file,"%s %s %s %.2f\n",emp.last_name,salary);
}

// Starts the execution of the program.
int main(void) {
    FILE *employees_file = fopen("employees.txt","r");
    if (employees_file == NULL) {
        fprintf(stderr,"Error: File employees.txt was not found.\n");
        exit(EXIT_FAILURE);
    }
    FILE *payroll_file = fopen("payroll.txt","w");

    Employee employees = read_data(employees_file);

    float salary;

    while (! feof(employees_file)) {
        salary = weekly_salary(employees);
        write_data(payroll_file,employees,salary);
        employees = read_data(employees_file);
    }

    fclose(employees_file);
    fclose(payroll_file);
    printf("All employees were read and payroll was written.\n");
    exit(EXIT_SUCCESS);
}

我正在尝试确定Employee的类型,以便程序可以正确计算Weekly_Salary,并在输出文件(payroll.txt)中使用薪水而不是小时数和薪水率来创建和打印该雇员的数据

这是数据:

H 1111简多(Jane Doe)8.50 40.0 S 2222约翰·多伊0.10 1500.00 高3333乔治·迈尔斯7.25 45.0 H 4444塔玛拉·史密斯8.25 42.5 S 5555玫瑰沃森0.15 2000.00

此外,以下代码我有用,但是我知道这不是最有效的方法,因为我使用字符串来仅捕获单个字符:

Employee read_data(FILE *in_file) {
    Employee emp;
    char ch[2];
    fscanf(in_file,"%s %s %s %s",emp.last_name);

    if(strcmp(ch,"H") == 0){
        emp.type = Hourly;
        fscanf(in_file,&emp.sales_emp.sales_amount);
    }

    return emp;
}

结果如下:

1111 Jane Doe 340.00
2222 John Doe 150.00
3333 George Miles 344.38
4444 Tamara Smith 360.94

很抱歉,如果这样可以获取更多信息

解决方法

感谢Kaylum随附解决该程序问题的答案。在读取FILE中的第一个字符时,我在 ch 之前缺少了&号,并且在格式说明符中还添加了行距“%c” ,因此它仅读取第一个字符每个换行符上的字符,而该行上没有其他字符。

// Reads the data of a employee from a file.
Employee read_data(FILE *in_file) {
    Employee emp;
    char ch;
    fscanf(in_file," %c %s %s %s",&ch,emp.id,emp.first_name,emp.last_name);

    if(cd == 'H'){
        emp.type = Hourly;
        fscanf(in_file,"%f %f",&emp.hourly_emp.pay_rate,&emp.hourly_emp.hours_worked);
    }
    else {
        emp.type = Sales;
        fscanf(in_file,&emp.sales_emp.commission_rate,&emp.sales_emp.sales_amount);
    }

    return emp;
} 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。