Reputation: 577
I'm trying to read file in c and store it in array. After that I need convert to integer related to input. But I got segmentation fault. When I'm looking for reason then I realised that strtok() causes segmentation fault. I tried some of solutions but I could not do that.
My input file is like:
1:2:3:4:5:6
6:5:4:3:2:1
and I store it in array.
Note that: I need to split it by ":" character. And every item must be digit.
then my code is below:
char* line = arr[i];
int d = atoi(strtok(line, ":"));
for ( j = 0; j < SIZE; j++) {
pushLinkedList(&tmp->P, d);
d= atoi(strtok(NULL, ":")); /* This part causes segmentation fault. */
}
How can i get rid of this fault? Thanks in advance.
Upvotes: 0
Views: 1061
Reputation: 12634
How can you be so sure that it's strtok()
which is causing segmentation fault?
Compile and run this program and check the behaviour:
#include <stdlib.h>
int main(void) {
atoi (NULL);
return 0;
}
From strtok [emphasis added]:
Return value
Returns pointer to the beginning of the next token or NULL if there are no more tokens.
Look at these statements:
for ( j = 0; j < SIZE; j++) {
pushLinkedList(&tmp->P, d);
d= atoi(strtok(NULL, ":"));
.....
.....
If SIZE
is some value greater than <number of tokens> - 1
then strtok()
will return NULL
for the nth iteration where n
is equal to <number of tokens>
in the string.
Since, you have not shown the value of SIZE
in code posted, I believe it's greater than the number of tokens in the string processed in the loop. When strtok()
finds all the tokens in the given string then all future calls to strtok()
will return a NULL
pointer and this NULL
pointer is passed to atoi()
which is resulting in segmentation fault. You should check the strtok()
return value and pass it to atoi
only when it's not a NULL
.
You can do:
int d;
char* line = arr[i];
char* tok = strtok(line, ":");
while (tok) {
d = atoi(tok);
pushLinkedList(&tmp->P, d);
tok = strtok(NULL, ":");
}
Additional:
Why you should not use atoi()
?
Upvotes: 4
Reputation: 48572
strtok
isn't causing the segfault. atoi
is, because you're passing it a null pointer. You need to check whether strtok
returns a null pointer and break out of your loop if it does, rather than just blindly handing it to atoi
.
Upvotes: 3