尝试从文本文件中读取数据,从中创建结构,然后将字段打印到stdout

时间:2022-12-24 09:43:42

I seem to have no problems reading from the file and then creating the struct, but printing the struct gives me a segmentation fault.

我似乎没有问题从文件读取然后创建结构,但打印结构给我一个分段错误。

Employee definition

struct _Employee {
  int salary; // Monthly salary in UK pounds sterling
  char *name; // Pointer to character string holding name of employee.
  char* department;           // MUST be dynamically allocated from the heap.
};

typedef struct _Employee Employee;

Function to read from a file

从文件中读取的功能

Employee* readfile(FILE* file) {
  Employee* newemployee;
  newemployee = malloc(sizeof(Employee));
  char tempsalary[10];
  int salary;
  char name[20];
  char dept[20];
  char* names = malloc(sizeof(name));
  char* depts = malloc(sizeof(dept));
  char* status; // Returned by fgets(). Will be NULL at EOF


    status = fgets(names, sizeof(name), file);
    if (status == NULL)
      return NULL;
    else {
      newemployee->name = strdup(status);


    fgets(tempsalary, sizeof(name), file);
    sscanf(tempsalary, "%d", &salary);
    newemployee->salary = salary;

    fgets(depts, sizeof(dept), file);
    newemployee->department = strdup(depts);

    return newemployee;
    }
}

Function to print the struct generated by readfile.

用于打印readfile生成的struct的函数。

void printEmployee(Employee *employee) {
      fprintf(stdout, "Name = %sSalary = %d\nDepartment = %s\n\n", // SEGFAULT HERE
          employee->name, employee->salary, employee->department);
}

Main program

int main() {
  FILE* file;
  file = fopen ("stest2.txt", "r");
  Employee* employees[max_employees];
  int i;
  int c;
  Employee* temp;

    for (i = 0; i < max_employees; i++) {
    employees[i] = readfile(file)   
    printEmployee(employees[i]);
    }
  return 0;
}

2 个解决方案

#1


0  

readfile() can return NULL in case of fgets error. This case is not handled in main. As a primitive suggestion:

如果fgets错误,readfile()可以返回NULL。这种情况不在主要处理。作为一个原始的建议:

    for (i = 0; i < max_employees; i++) {
        employees[i] = readfile(file);
        if(NULL != employees[i])
        {   
            printEmployee(employees[i]);
        }
        else
        {
            printf("Error reading file");
        }

#2


0  

I'll have to go with fgets() scribbling on the stack. Change this:

我必须用fgets()在堆栈上涂鸦。改变这个:

 fgets(tempsalary, sizeof(name), file);

to this:

 fgets(tempsalary, sizeof(tempsalary), file);

Might not be the problem, but it's certainly "a" problem.

可能不是问题,但它肯定是“一个”问题。

#1


0  

readfile() can return NULL in case of fgets error. This case is not handled in main. As a primitive suggestion:

如果fgets错误,readfile()可以返回NULL。这种情况不在主要处理。作为一个原始的建议:

    for (i = 0; i < max_employees; i++) {
        employees[i] = readfile(file);
        if(NULL != employees[i])
        {   
            printEmployee(employees[i]);
        }
        else
        {
            printf("Error reading file");
        }

#2


0  

I'll have to go with fgets() scribbling on the stack. Change this:

我必须用fgets()在堆栈上涂鸦。改变这个:

 fgets(tempsalary, sizeof(name), file);

to this:

 fgets(tempsalary, sizeof(tempsalary), file);

Might not be the problem, but it's certainly "a" problem.

可能不是问题,但它肯定是“一个”问题。