将文件读入结构,但无法读取文件中的第一行

Reading file into Struct, but cannot read past first line in file

我正在处理一项任务,我需要根据员工姓氏按字母顺序对结构进行排序。我已经编写了程序并且可以 运行,但不幸的是它似乎正在删除结构中的所有姓氏。

现在程序将文件中包含的 40 名员工的数据读取到结构中。

我是不是在排序功能中遗漏了什么?

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

typedef struct {
    int employee_code;
    char *employee_surname[20];
    char *employee_forname[20];
    int insurable_weeks;
    float gross_pay;
    float subsidy;
} employee;

employee tempEmployee[100];

void load_data(fp);
void display_menu();
void view_all_employees();
void add_new_employee();
void delete_employee();
void view_qualifying_employees();
void find_disqualified_employees();
void sort_employees_by_subsidy_amount();
void view_total_subsidy_amount();
void sort_employees_by_name();

int main(void)
{
    FILE* fp = fopen("data.txt", "r");;

    int selection=0, state;
    int n=0;

    load_data(fp);//open file if it exists. If not, create new file. 

    fclose(fp);

    printf("Please choose an option (1-9)...\n");
    do {
        display_menu();
        //scanf("%d", &selection);

        if (state = (scanf("%d", &selection)) != 1) //validates that user is only entering integers.
        {
        printf("\n\n**** Please only enter numbers. Character strings are not accepted. ****\n\n");
        while (getchar() != '\n'); //clears user input to avoid infinite loop. 
        }

        else if (selection < 0 || selection >9)
        {
            printf("\nInvalid entry. Only enter digits between 1-9,\n");
        }

        else
        {
            switch (selection)
            {

            case 1:
                add_new_employee();//view new employees
                break;

            case 2:
                delete_employee();//delete employees
                break;

            case 3:
                view_all_employees();//view all employees and related info
                break;

            case 4:
                view_qualifying_employees();//view employees qualifying for subsidy
                break;

            case 5:
                find_disqualified_employees();//find employees disqualified for subsidy
                break;

            case 6:
                sort_employees_by_name();//sort employee by surname
                break;

            case 7:
                sort_employees_by_subsidy_amount();
                break;

            case 8:
                view_total_subsidy_amount();//total subsidy amount for all employees
                break;



            //more cases added later

            }
        }

    } while (selection != 9);

    printf("\nThank you for using the program! Terminating...\n");

    return 0;

}

这是导致问题的函数:

void sort_employees_by_name()
{
    int n = 100, i, j;
    char temp[20];

    for (i = 0; i <= n; i++)
    {
        for (j = 0; j <= n - i; j++)
        {
            if (strcmp(tempEmployee[j].employee_surname, tempEmployee[j + 1].employee_surname) > 0)
            {
                strcpy(temp, tempEmployee[j].employee_surname);
                strcpy(tempEmployee[j].employee_surname, tempEmployee[j + 1].employee_surname);
                strcpy(tempEmployee[j + 1].employee_surname, temp);
            }
        }
    }
}

你必须存储名称的声明是一个指向字符的指针数组

char *employee_surname[20];
char *employee_forname[20];

但你实际需要的只是一个字符数组

char employee_surname[20];
char employee_forname[20];

关于记录数,最好将计数存储在某个变量中或读取文件直到看到 EOF

for (i = 0; i <= n; i++) 是错误的,因为您试图读取的内容超过文件中的内容,获取 n 的实际值并使用 for (i = 0; i < n; i++)