Sscanf does not detect zeros

I am having a hard time using sscanf to scan hours and minutes from a list. Below is a small list of the list.

1604 124 12:05p 1:21p Daily
1605 124 1:20p 2:40p Daily
1606 173 3:15p 4:38p Daily
1607 173 4:20p 5:43p Daily
1608 124 8:20p 10:00p Daily
1609 124 9:00p 10:37p Daily
1610 173 8:40a 10:05a Daily
1611 124 10:50p 12:20a Daily
1701 17 9:25a 1:00p Daily
1702 17 10:10a 1:45p Daily
1703 86 1:55p 5:15p Daily
1704 86 2:30p 5:50p Daily
1711 17 10:40a 2:15p 5
1712 86 3:10p 6:30p 1
1731 48 6:25a 9:30a 156
1732 100 10:15a 1:30p Daily
1733 6 2:15p 3:39p Daily

      

I tried this, but for a while, it casts null, which is very important. (I put this information into structures).

    put_flights_into_array(int size){

      int i,check_enter,x,c;

      check_enter=0;
      x=0;

      /*Puts the temp_flights to all_flights*/

      for (i=0;i<size;i++){
        if(temp_flights[i] == '\n'){
          all_flights[check_enter][x]='\0';
          check_enter++;
          x=0;
        }else{
          all_flights[check_enter][x]=temp_flights[i];
          x++;
        }
      }

      /*Puts all_routes into proper structures*/

      for(i=0;i<check_enter;i++){
        sscanf(all_flights[i],
        "%d %d %d:%d%c %d:%d%c %s",
        &all_flights_divid[i].flight_number,
        &all_flights_divid[i].route_id,
        &all_flights_divid[i].departure_time_hour,
        &all_flights_divid[i].departure_time_minute,
        &all_flights_divid[i].departure_time_format,
        &all_flights_divid[i].arrival_time_hour,
        &all_flights_divid[i].arrival_time_minute,
        &all_flights_divid[i].arrival_time_format,
        &all_flights_divid[i].frequency);
        }

      

This is how I declared my structure.

struct all_flights{
  int flight_number;
  int route_id;
  int departure_time_hour;
  int departure_time_minute;
  char departure_time_format;
  int arrival_time_hour;
  int arrival_time_minute;
  char arrival_time_format;
  char frequency[10];
};
struct all_flights all_flights_divid[3000];

      

These are the results I am getting. Notice how in 1604 the time is 12 5, it should be 12 05, What happens to the zero before five? The same thing happens at 1609, the first zero has disappeared.

1604 124 12 5 p 1 21 p Daily
1605 124 1 20 p 2 40 p Daily
1606 173 3 15 p 4 38 p Daily
1607 173 4 20 p 5 43 p Daily
1608 124 8 20 p 10 0 p Daily
1609 124 9 0 p 10 37 p Daily
1610 173 8 40 a 10 5 a Daily
1611 124 10 50 p 12 20 a Daily
1701 17 9 25 a 1 0 p Daily
1702 17 10 10 a 1 45 p Daily
1703 86 1 55 p 5 15 p Daily
1704 86 2 30 p 5 50 p Daily
1711 17 10 40 a 2 15 p 5
1712 86 3 10 p 6 30 p 1
1731 48 6 25 a 9 30 a 156
1732 100 10 15 a 1 30 p Daily
1733 6 2 15 p 3 39 p Daily

      

+3


source to share


1 answer


the problem is not scanning, but printing. if you use a format like this

printf("%02d", n);

      



that zero will make sure there is zero zero as you want

+5


source







All Articles