Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
读取不带'';t从文件中不存在-C_C_Printf_Scanf - Fatal编程技术网

读取不带'';t从文件中不存在-C

读取不带'';t从文件中不存在-C,c,printf,scanf,C,Printf,Scanf,我正在尝试读取已写入的文件。 文本文件包含: 1 VA1 01 01 01 01 BAS 01 01 01 01 其中1是存储在文件中的航班数,下面的所有内容都是航班详细信息。我想知道如何读取并将第一个字符转换为整数,然后使用整数进行for循环,打印下面的行数 flight_t loadFlights(char flightDatabase[50], int totalflights, flight_t f[MAX_NUM_FLIGHTS]) { int counter; FI

我正在尝试读取已写入的文件。
文本文件包含:

1  
VA1 01 01 01 01 BAS 01 01 01 01  
其中1是存储在文件中的航班数,下面的所有内容都是航班详细信息。我想知道如何读取并将第一个字符转换为整数,然后使用整数进行for循环,打印下面的行数

flight_t loadFlights(char flightDatabase[50], int totalflights, flight_t f[MAX_NUM_FLIGHTS]) {
  int counter;
  FILE * fp;
  fp = fopen("database.txt", "r");

  if (fp == NULL) {
    printf("Read error\n");
    return *f;
  }

  sscanf(fp, "%d", & totalflights);

  for (counter = 0; counter <= totalflights; counter++) {
    fscanf(fp, "%s %d %d %d %d %s %d %d %d %d", f[counter].flightcode, & f[counter].departure_dt.month, & f[counter].departure_dt.date, & f[counter].departure_dt.hour, & f[counter].departure_dt.minute,
      f[counter].arrival_citycode, & f[counter].arrival_dt.month, & f[counter].arrival_dt.date, & f[counter].arrival_dt.hour, & f[counter].arrival_dt.minute);
    printf("%s %02d %02d %02d %02d %s %02d %02d %02d %02d\r\n",
      f[counter].flightcode, f[counter].departure_dt.month, f[counter].departure_dt.date,
      f[counter].departure_dt.hour, f[counter].departure_dt.minute,
      f[counter].arrival_citycode, f[counter].arrival_dt.month,
      f[counter].arrival_dt.date, f[counter].arrival_dt.hour,
      f[counter].arrival_dt.minute);
    totalflights++;
  }

  printf("%d\n", totalflights);
  fclose(fp);
  return *f;

}

关于:
sscanf(fp、%d)和totalflights)此语句有一些问题。1) 
sscanf()
用于从内存中已有的缓冲区中读取,而不是从文件中读取。2)
totalflights
是参数列表中的一项(即在调用函数调用块上),因此地址是该函数调用块上的某个位置,而不是调用方函数中数据的地址。也就是说,调用者应该已经在其函数中传递了数据的地址和
loadFlights()的签名
应该说:
int*totalflights
关于国产类型:
flights\u t
未在发布的代码中定义。为了便于阅读和理解:1)始终缩进代码。在每个左大括号“{”之后缩进。在每个右大括号“}”之前取消缩进。建议每个缩进级别为4个空格。2) 变量(和参数)名称应表示
内容
用法
即使在目前的情况下,f'`也毫无意义。3) 通过拆分语句和缩进其他行来遵守右边距(第72列或第80列)。调用任何
scanf()
函数系列时:1)始终检查返回值(而不是参数值),以确保操作成功。注意:这些函数没有设置任何有用的
errno
,因此需要使用:
fprintf(stderr,“…\n”,…)
以报告错误。2) 使用输入格式说明符“%s”和/或“%[…]”时,始终使用比输入缓冲区长度小1的最大字段宽度修饰符,因为这些输入说明符始终向输入追加NUL字节。这避免了缓冲区溢出和随之而来的未定义行为的任何可能性
struct flight {
    char flightcode[MAX_FLIGHTCODE_LEN+1]; /*Char as the code e.g. 125620 will be read as characters not an integer as you dont need to apply any mathematics to the code*/
    char arrival_citycode[MAX_CITYCODE_LEN+1]; /*Add +1 to leave room for the null character /0 so you get you full word length rather than 'fligh' */
    date_time_t departure_dt; /*use the date structure types*/
    date_time_t arrival_dt; /*same as above*/
 };
 typedef struct flight flight_t;