我正在尝试读取一个非常简单的数据文件,如下所示:
1597 1 0 3 1
使用以下代码:
void boot(){
FILE *f = fopen("shutdown.txt", "r");
uint8_t timestamp = 0;
uint8_t power_down_type = 0;
uint8_t power_down_cause = 0;
uint8_t boot_number = 0;
uint8_t antenna_deployed = 0;
uint8_t images_captured = 0;
fscanf(f, "%u %d %d %d %d %d", ×tamp, &power_down_type, &power_down_cause, &boot_number, &antenna_deployed, &images_captured);
printf("timestamp: %un", timestamp);
printf("power_down_type: %dn", power_down_type);
printf("power_down_cause: %dn", power_down_cause);
printf("boot_number: %dn", boot_number);
printf("antenna_deployed: %dn", antenna_deployed);
printf("images_captured: %dn", images_captured);
}
但是当我运行代码时,我收到一个 SEGV 错误。
%d
读取一个int
,%u
读取一个unsigned int
,两者都大于uint8_t
。 通过写入超过变量的大小来发生未定义的行为。 %hhu
应该用于uint8_t
。