I want to tokenize a provided input string using strtok(). In the first step, I want to tokenize the given string by "|" symbol. Then, I tokenize each substring by ";". Finally, I tokenize the resulting substrings by " "(empty char). At the end, I aim to obtain an array of string arrays.
However, my while loop iterates just one time so I cannot process the whole string. All I get is:
I duplicate substrings before I pass them to strtok() but it does not work. I have spent almost 3 days but I could not achieve it yet. Any help will be greatly appreciated. For more detail, I provide the code and the output I receive.
Output: cat my_books.txt[enter image description here
The function
strtokhas a static local variable (pointer) that stores the last position of the parsed string within the function.For example after the inner most loop
the static variable will be equal to NULL. So all outer loops will stop their iterations.
Instead use function
strtok_sfor which you can specify an argument that will keep the last position in the parsed string.The function is declared like
So for each loop you can declare a pointer like for example
and pass it to the function as expression
&pos.