读取空文件时fscanf崩溃

读取空文件时fscanf崩溃,c,scanf,satellite,uclinux,C,Scanf,Satellite,Uclinux,我正在从事一个大型项目,该项目有一个读取数据文件的函数。但是,在某些测试代码中,该文件不存在,因此在创建该文件时,它会创建一个空文本文件。我编写了以下代码来补偿此事件: 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_cap

我正在从事一个大型项目,该项目有一个读取数据文件的函数。但是,在某些测试代码中,该文件不存在,因此在创建该文件时,它会创建一个空文本文件。我编写了以下代码来补偿此事件:

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做的事情?有人能帮我吗

fscanf third和next的参数必须是指向适当类型的指针。在最简单的情况下,可以使用&运算符

难题,没有从较短的int类型字符/字节自动转换

ret = fscanf(f, "%s %d %d %d %d %d %d",
  bootstatus->timestamp, // string
  &bootstatus->power_down_type, // int
...
  );
这取决于声明,在我的示例中只允许int声明

如果没有整数,请使用临时变量。在本例中,时间戳是不同类型的整型字节,以此类推

   int tmp1;

    ret = fscanf(f, "%s %d %d %d %d %d %d",
      bootstatus->timestamp,
      &tmp1 ,
    ...
      );

    bootstatus->power_down_type = tmp1;
违反这些规则会带来严重的问题,这取决于系统、编译器等

我的答案是基于假设,而不是这个结构的真实声明,在编写时未知

您正在将一个int-by-value传递给接受变量int*指针的函数

只要简单地传递一个变量地址,它就不应该再冻结了。 您可以通过撤销变量地址&var来获取变量地址


但不是对弦!尽管×tamp指向与timestamp相同的地址。

这些变量(如power\u down\u type)有哪些类型?智力?int*?我们如何知道文件正确打开?我们如何知道运算符的缺失和地址是正确的,并且结构包含指针?请发布显示问题的。在第二个块中,您有bootstatus->power\u down\u type=0;这表明它们是值而不是指针。typedef结构系统启动状态{char timestamp[18];int power down类型;int power down原因;int boot号码;int天线部署;int图像捕获;int信标计数;}系统启动状态;投票因输入错误而关闭原因:除了fscanf的第一个目标参数外,所有运算符都缺少和地址。请编辑您的问题,并将struct system_boot_状态定义放在那里。时间戳是确定的,它是一个字符串,而不是注释中的//int。我已更改答案它被读取为%s,它可能会起作用,因为指向数组的指针与指向第一个字符的指针相同,但用法不正确。agree@FilipKočica。我的回答是在没有声明的情况下,甚至在我写第一条评论之前就有%s:-编辑:不用管它是固定的,不要再为它操心了。
  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);
  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);