strtok et cie sont du C, si tu veux utiliser des tableaux dynamiques en C, il faut utiliser malloc et realloc, ca donnerait un truc de ce genre:
char ** split (char *str, const char *delim)
{
char **result = (char **) calloc (1, sizeof (char *));
unsigned int i=0;
char *pch = strtok (str," " );
while (pch != NULL)
{
result = (char **) realloc (result, (i+2) * sizeof (char *));
result[i] = pch;
i++;
pch = strtok (NULL, " " );
}
result[i]=NULL;
return result;
}
(en rajoutant les controle d'erreurs et ne pas oublier de libérer la mémoire)
cette fonction modifie la chaine passée en paramètre
en C++:
#include <string>
#include <iostream>
#include <vector>
using namespace std;
vector <string> split (string str, char delim)
{
vector<string> str_list;
istringstream iss(str);
string token;
while (getline (iss, token, delim))
{
str_list.push_back (token);
}
return str_list;
}
int main()
{
string buffer="1 2 3 4";
vector<string> tab;
unsigned int i=0;
tab = split (buffer, ' ');
for (i=0; i<tab.size (); i++)
cout << tab[i] << endl;
return 0;
}
sinon, tu as aussi la classe boost/tokeniser