I'm trying to tokenize a string taken from a file. strtok_r works properly on the first sub string and then returns null (and segmentation fault cause I try to strndup into an other var)
char buffer[500];
char * c;
char * c1;
char * c2;
//....
while(fgets(buffer, sizeof(buffer), f) != NULL){
c2 = buffer;
printf("%s\n", buffer);
c = strtok(c2, ":");
for(int i = 0; i < 4; i++){
c = strtok(NULL, ":");
printf("%s\n", c);
}
if(strcmp(c, argp->origen) == 0){
c = strtok(NULL, ":");
printf("%s\n", c);
if(strcmp(c, argp->destino) == 0){
nodo = malloc(sizeof(lista_vuelo));
c2 = buffer;
c = strtok_r(c2, ":", &c1);
nodo->IdReg = atoi(c);
printf("\n%d test\n", nodo->IdReg); //Works until here
c = strtok_r(NULL, ":", &c1);
printf("\n%s\n", c); //Prints null and then segmentation fault
nodo->Idvuelo = strndup(c, strlen(c));
printf("\n%s\n", nodo->Idvuelo);
//....
Input from file :
3:IBE3674:02-04-2019:19-45:Madrid:Berlin:Barajas:Tegel:IBERIA:210:35:6:T4:60:N
Output :
3 test -> Expected output
(null)
Violación de segmento (`core' generado) -> Segmentation fault, (null) should be IBE3674
strtok
does not simply modify the pointer it is passed but actually the string itself. It replaces each found delimiter with a null
character.
If you had string test:strtok:for:me
and call strtok
once you had test\0strtok:for:me
after that.
So when you iterate ofer the first couple tokens each :
is replaced with \0
. If you now reset your pointer c2
to the beginning of the string and call strtok again, strtok finds a null
charakter before it finds a delimiter and assumes the string ended before a delimiter is found and returns NULL
.