读取文件中不存在的数字 - C.

时间:2018-05-04 10:01:04

标签: 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;
  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它正在打印此错误: Error

a)如何将第一个角色变成一个int b)如何从文本文件的第二行开始打印。

ERROR USING fscanf

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;

0 个答案:

没有答案