我正在开发一个具有读取数据文件功能的大型项目。然而,在某些测试代码中,该文件不存在,因此在创建它时,它会创建一个空文本文件。我编写了以下代码来补偿此事件:
typedef struct system_boot_status_s{
char timestamp[18];
int power_down_type;
int power_down_cause;
int boot_number;
int antenna_deployed;
int images_captured;
int beacon_count;
}system_boot_status_t;
////////////////////////////////
// Read the boot status info into the boot status struct
ret = fscanf(f, "%s %d %d %d %d %d %d",
bootstatus->timestamp,
bootstatus->power_down_type,
bootstatus->power_down_cause,
bootstatus->boot_number,
bootstatus->antenna_deployed,
bootstatus->images_captured,
bootstatus->beacon_count);
if (ret != 7) // if 7 items weren't read
{
// Make sure all boot status members are set to 0
snprintf(bootstatus->timestamp, BOOT_INFO_LEN, "xx-xx-xx-xx-xx-xx");
bootstatus->power_down_type = 0;
bootstatus->power_down_cause = 0;
bootstatus->boot_number = 0;
bootstatus->antenna_deployed = 0;
bootstatus->images_captured = 0;
bootstatus->beacon_count = 0;
return -1;
}
我知道 fscanf 返回它读取的东西的数量,但是当我运行这个程序并且它到达空文件时,我的程序只是冻结了。我错过了我应该用 EOF 做的事情吗?谁能帮我吗?