Hey guys I'm getting a seg fault with strtok, just need a little bit of help!
char s[1024];
char *token[2];
while(fgets(s, sizeof(s), fp) != NULL) // Read line from file fp until end
{
token[0] = strtok(s, "\t\n");
token[1] = strtok(NULL, "\t\n");
token[2] = strtok(NULL, "\t\n");
printf("%d:%s:%d", atoi(token[0]), token[1], atoi(token[2]));
}
But I get a seg fault after the first passover of the loop. The input file reads something like this:
102910319 code mark
.
.
.
104981851 code mark
But the while loop only prints the first line correctly and seg faults on the second line.
Any ideas why?
Thanks.
token[2]
but you access 3 elements – Jacktoken[1]
andtoken[2]
, do we require\t\n
or only\t
is sufficient as the delimiter? – Ganesh