我很难找出seg故障的原因。我在GDB中进行了调试,它告诉我这条线给我带来了麻烦,但我仍然无法弄清楚。作业分段错误。不知道为什么
Employee* readfile(FILE* file) {
Employee* newemployee;
char* tempsalary;
int salary;
char* name;
char* dept;
char line[128];
while(file != NULL) {
fgets(name, sizeof(line), file);
newemployee->name = strdup(name); // THIS IS WHERE THE SEGFAULT IS
fgets(dept, sizeof(line), file);
newemployee->department = strdup(dept);
fgets(tempsalary, sizeof(line), file);
sscanf(tempsalary, "%d", &salary);
newemployee->salary = salary;
}
return newemployee;
我试图运行它的主程序应该打开文件,读取行并创建一个Employee结构。它使用以前的函数打印结构。
int main() {
FILE* file;
file = fopen ("stest2.txt", "r");
Employee* employees[max_employees];
int i;
int c;
for (i = 0; i < max_employees; i++) {
employees[i] = readfile(file);
printEmployee(employees[i]);
}
}
我解决了导致seg错误的问题,但现在程序运行时没有打印结果并结束。有任何想法吗? – 2013-02-12 04:17:02