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 ?
These statements
are valid and can not result in a segmentation fault provided that
location_tempis not equal toNULLand 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
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
strtokbefore 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..