How does strtok work exactly?

734 Views Asked by At

I have a file that i want to read from in c, the file's format is as follows :

<city> <pokemon1> <pokemon2>; <near_city1> <near_city2>

for example: paris pidgey pikachu; london berlin I want to be able to cut this row into tokens using strtok, but for some reason its not working properly.

My code: lets say I have this row read from the file using fgets and put into char* row. so what I did was :

char* city_name = strtok(location_temp, " "); // to receive city
char* pokemons_names = strtok(strtok(location_temp, " "),";");

Although , this code brings segmentation fault later on, so I followed the debugger and noticed that the second code line is not being executed properly.

Help ?

2

There are 2 best solutions below

0
On

You can use strtok() like this to gather your information about each line:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    char string[] = "paris pidgey pikachu; london berlin";

    char *city = strtok(string, " ");
    printf("city: %s\n", city);

    char *pokemon_names = strtok(NULL, ";");
    printf("pokemon names: %s\n", pokemon_names);

    char *near_cities = strtok(NULL, "\n");
    memmove(near_cities, near_cities+1, strlen(near_cities)); /* removes space at start of string */
    printf("near cities: %s\n", near_cities);

    return 0;
}

Output:

city: paris
pokemon names: pidgey pikachu
near cities: london berlin

It really depends how your storing these strings though.

0
On

These statements

char* city_name = strtok(location_temp, " "); // to receive city
char* pokemons_names = strtok(strtok(location_temp, " "), ";");

are valid and can not result in a segmentation fault provided that location_temp is not equal to NULL and does not point to a string literal.

However this code snippet does not do what you expect. The first and the second statements return the same pointer that is the address of the initial word in the string pointed to by location_temp.

You should write at least like

char* city_name = strtok(location_temp, " "); // to receive city
strtok(NULL, " ");
char* pokemons_names = strtok( NULL, ";");

I think that the segmentation fault occurs because you do not copy the resulted strings in separate character arrays. But without your actual code it is difficult to name the reason exactly.

You should read the description of the function strtok before using it.

Take into account that the original string is changed within the function by inserting the terminating zero for the extracted substring and the function keeps the address of the next part of the original string after the inserted terminating zero until it will be called with the first argument unequal to NULL..