C - Segmentation fault with strtok() -
i trying date console , getting month, day , year work them separately.
const size_t max = 11; void getdate(date * d){ char line[max]; printf("\t insert date in american format (mm/dd/yyyy): "); fgets(line, max, stdin); d->month = atoi(strtok(line, "/")); d->day = atoi(strtok(null, "/")); d->year = atoi(strtok(null, " ")); }
i don't error executing once. segmentation fault error when seek 2 dates @ once.
date d1, d2; getdate(&d1); getdate(&d2);
and line gives me error d->day = atoi(strtok(null, "/"));
during sec execution.
the problem utilize of fgets()
. not returning think sec time around.
the first time through, fgets()
fills line[]
"10/23/2014\0"
, well.
however, sec time through, enter key still in stdin
's input buffer because first fgets()
did not have room in line[]
read it, sec fgets()
fills line[]
"\n\0"
without waiting new user input. first phone call strtok(line, "/")
returns "\n"
(which atoi()
converts 0), next phone call strtok(null, "/")
fails , returns null, causes atoi()
segfault.
increase size of array enter read each phone call fgets()
. suggest utilize sscanf()
instead of atoi(strtok())
:
const size_t max = 16; void getdate(date * d) { char line[max]; printf("\t insert date in american format (mm/dd/yyyy): "); fgets(line, max, stdin); if (sscanf(line, "%d/%d/%d", &(d->month), &(d->day), &(d->year)) != 3) d->month = d->day = d->year = 0; }
alternatively, add together validation create sure date read properly:
const size_t max = 16; void getdate(date * d) { char line[max]; int consumed; printf("\t insert date in american format (mm/dd/yyyy): "); fgets(line, max, stdin); while (1) { if (sscanf(line, "%d/%d/%d%n", &(d->month), &(d->day), &(d->year), &consumed) == 3) { if (consumed == strlen(line)) break; } printf("\t invalid input. insert date in american format (mm/dd/yyyy): "); fgets(line, max, stdin); } }
c strtok
No comments:
Post a Comment