我有以下枚举和struct:为什么在feof()中出现分段错误?
enum Destination { unknown = 0, hosok, parlament, var };
struct Client
{
char name[30];
char email[30];
char phone_num[11];
int client_num;
enum Destination destination;
struct tm registration_date;
};
当我调用下面的方法,它会读取第一个结构并打印它的名字,然后我得到一个分段错误。
void list_clientss()
{
FILE *f = fopen(filename, "r");
if(f == NULL)
{
perror("Error");
}
struct Client client;
while(!feof(f))
{
fread(&client, sizeof(struct Client), sizeof(struct Client), f);
printf("Name: %s\n", client.name);
}
fclose(f);
}
我错了什么?
[为什么“while(!feof(file))”总是出错?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) –
你确定它在'feof'中进行了吗?你的'fread'电话是错误的,你正在摧毁你的筹码。你应该调用'FREAD(客户端的sizeof(结构客户端),1,F);' – Cornstalks
你'型结构Client'的准备_1_记录。说'FREAD(客户端的sizeof(结构客户端),1,F);' – Gene