分段错误发生在代码的非常直接的部分
当我第一次运行该程序时(尚未创建输出文件),它工作正常。但是当我再次运行它(输出文件存在)时,它在读取最后一个输入后给出分段错误。
从问题来看,它似乎与我的文件处理有关,但是当我尝试调试时,我发现我得到了 seg.在退出 main()
中的 for 循环之前发生错误。
#include <stdio.h>
#define EMPS 3
struct employee
{
char firstname[40];
char lastname[40];
int id;
};
typedef struct employee Employee;
/* Input the employee data interactively from the keyboard */
void InputEmployeeRecord(Employee *ptrEmp);
/* Display the contents of a given Employee record */
void PrintEmployeeRecord(const Employee e);
/* Save the contents of the employee record list to the newly
created text file specified by FileName */
void SaveEmployeeRecord(const Employee e[], const char *FileName);
int id = 0;
int main()
{
Employee employees[EMPS];
for(int i = 0; i < EMPS; i++)
{
InputEmployeeRecord(&(employees[i]));
PrintEmployeeRecord(employees[i]);
}
SaveEmployeeRecord(employees, "employee.dat");
return 0;
}
void InputEmployeeRecord(Employee *ptrEmp)
{
printf("Enter first name\n");
scanf("%s", ptrEmp->firstname);
printf("Enter last name\n");
scanf("%s", ptrEmp->lastname);
ptrEmp->id = ++id;
}
void PrintEmployeeRecord(const Employee e)
{
printf("Employee %d: %s %s\n", e.id, e.firstname, e.lastname);
}
void SaveEmployeeRecord(const Employee e[], const char *FileName)
{
FILE* fptr = fopen(FileName, "r");
/* File doesn't exist */
if(fptr == NULL)
{
fclose(fptr);
// Create the file
FILE* fptr2 = fopen(FileName, "w");
fclose(fptr2);
// continue reading
fptr = fopen(FileName, "r");
}
char firstLetter;
int headerExists = 0;
if(!feof( fptr ))
{
fscanf(fptr, "%c", firstLetter);
if(firstLetter == 'I')
headerExists = 1;
}
fclose(fptr);
FILE* fptr2 = fopen(FileName, "a");
if(!headerExists)
fprintf(fptr2, "ID FIRSTNAME LASTNAME");
for(int i = 0; i < EMPS; i++)
fprintf(fptr2, "\n%d %s %s", e[i].id, e[i].firstname, e[i].lastname);
fclose(fptr2);
}
[代码最初位于http://pastebin.com/tLefJhEH]
When I run the program the first time (the output file is not yet created), it works fine. But when I run it again (output file exists), it gives a segmentation fault after reading the last input.
From looking at the problem, it looks like it has something to do with my file handling, but when I tried to debug, I found out that I get the seg. fault before exiting the for loop in main()
.
#include <stdio.h>
#define EMPS 3
struct employee
{
char firstname[40];
char lastname[40];
int id;
};
typedef struct employee Employee;
/* Input the employee data interactively from the keyboard */
void InputEmployeeRecord(Employee *ptrEmp);
/* Display the contents of a given Employee record */
void PrintEmployeeRecord(const Employee e);
/* Save the contents of the employee record list to the newly
created text file specified by FileName */
void SaveEmployeeRecord(const Employee e[], const char *FileName);
int id = 0;
int main()
{
Employee employees[EMPS];
for(int i = 0; i < EMPS; i++)
{
InputEmployeeRecord(&(employees[i]));
PrintEmployeeRecord(employees[i]);
}
SaveEmployeeRecord(employees, "employee.dat");
return 0;
}
void InputEmployeeRecord(Employee *ptrEmp)
{
printf("Enter first name\n");
scanf("%s", ptrEmp->firstname);
printf("Enter last name\n");
scanf("%s", ptrEmp->lastname);
ptrEmp->id = ++id;
}
void PrintEmployeeRecord(const Employee e)
{
printf("Employee %d: %s %s\n", e.id, e.firstname, e.lastname);
}
void SaveEmployeeRecord(const Employee e[], const char *FileName)
{
FILE* fptr = fopen(FileName, "r");
/* File doesn't exist */
if(fptr == NULL)
{
fclose(fptr);
// Create the file
FILE* fptr2 = fopen(FileName, "w");
fclose(fptr2);
// continue reading
fptr = fopen(FileName, "r");
}
char firstLetter;
int headerExists = 0;
if(!feof( fptr ))
{
fscanf(fptr, "%c", firstLetter);
if(firstLetter == 'I')
headerExists = 1;
}
fclose(fptr);
FILE* fptr2 = fopen(FileName, "a");
if(!headerExists)
fprintf(fptr2, "ID FIRSTNAME LASTNAME");
for(int i = 0; i < EMPS; i++)
fprintf(fptr2, "\n%d %s %s", e[i].id, e[i].firstname, e[i].lastname);
fclose(fptr2);
}
[ Code originally at http://pastebin.com/tLefJhEH ]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题可能出在
fscanf(fptr, "%c", firstLetter);
上。当您给它一个 char (在 c 中是一个整数)时,%c 期望一个 char* 作为输入。要修复它,请尝试
fscanf(fptr, "%c", &firstLetter);
另外,不建议在空指针上调用 fclose。
The problem probably lies at the
fscanf(fptr, "%c", firstLetter);
. %c expects a char* as input while you are giving it a char (which is an integer in c).To fix it try
fscanf(fptr, "%c", &firstLetter);
Also calling fclose on a null pointer is not recommended.
我的错误,是这一行:
应该是:
fscanf
和scanf
的参数是存储数据的内存位置的地址。字符串不需要
&
,因为字符串已经是一个地址。My mistake, it's this line:
Should be:
Argments to
fscanf
andscanf
are addresses of the memory locations to store the data.You don't need the
&
for a string because a string is already an address.