Użycie Strtok, kod nie działa [duplikat]
To pytanie ma już tutaj odpowiedź:
zachowanie strtok 2 odpowiedziPróbuję użyćstrtok()
. Oto fragment kodu, który napisałem. Nie działa, ale drukuje", '"
nieskończenie.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
char str[]="this, by the way, is a 'sample'";
char *tokens;
tokens = strtok(str, ", '");
//printf("%s\n",tokens);
//printf("%s\n", str);
while(tokens!=NULL)
{
printf("%s\n", tokens);
tokens = (NULL, ", '");
}
return 0;
}
Poniżej znajduje się kod z astrtok()
strona podręcznika, która działa doskonale.
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
return 0;
}
Czuję, że zrobiłem dokładnie to samo. Nie mogę znaleźć błędu w moim kodzie. Czy ktoś mógłby wskazać.