comportement de la fonction strtok()
Je ne comprends pas pourquoi ce bout de code ne marche pas (Erreur de segmentation).
Code:
1 2 3 4 5 6 7 8 9
| 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, " ,.-");
} |
C'est la même chose que ce qu'il y a là mais en changeant char str[] en char *str. Je pensais que c'était la même chose, les pointeurs et les tableaux...
Le code original :
Code:
1 2 3 4 5 6 7 8 9
| 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, " ,.-");
} |
En fait il faudrait que je fasse passer un pointeur vers une chaîne en paramètre (comme rvalue de str=...). Ça donne :
Code:
1 2 3 4 5 6 7 8 9 10
| char *str;
strcpy(str,nomFichier);
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
} |
Et là, c'est à peine mieux :
Citation:
Splitting string "../data/video/campus_raw.AVI" into tokens:
1
/data/video/campus_raw
2
AVI
Erreur de segmentation (core dumped)