Я пытаюсь прочитать файл, в который я записал.
Текстовый файл содержит:
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](https://i.stack.imgur.com/EQukZ.png)
а) как я могу получить первый символ в int
б) как начать печать со второй строки в текстовом файле.
![ERROR USING fscanf](https://i.stack.imgur.com/5wtD1.png)
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;