Estoy escribiendo una consola en C, pues debo recibir comandos (por el teclado) y parsearlos.
E.j: tengo este comando:
echo hola! -f
Pues nececitaria separar esas strings algo asi:
["echo", "hola!", "-f"]
Osea separarlas en diferentes strings y guardarlas en un array.
Como lo podria hacer?
Saludos
Tu funcion se llama strtok
https://en.cppreference.com/w/c/string/byte/strtok
Gracias! :)
char* cmdparser(char* fstrlv1, char* s, int len)
{
char* fstrlv[len];
char *tok = s;
int i = 0;
while ((tok = strtok(tok, " ")) != NULL)
{
fstrlv[++i] = tok;
tok = NULL;
}
fstrlv[i] = '\0';
return fstrlv;
}
He tratado de escribir esta función que debería retornarme el array con los strings separados, pero no funciona...
Algo me dice que mi código es horrible...
El ++i no deberia ser i++?
Tampoco funciona :/
EDIT: Disculpa no habia chequeado tu code :silbar: en fin
#include <iostream>
#include <cstring>
#include <vector>
std::vector<char*> cmdparser(char* str, const char *delim)
{
std::vector<char*> str_array;
char *token = strtok(str, delim);
while (token)
{
str_array.push_back(token);
token = strtok(NULL, delim);
}
return str_array;
}
int main()
{
char str[] = "-param_1 -param_2 -param_3 -param_n";
char delim[] = " ";
for(auto s : cmdparser(str, delim))
cout << s << endl;
return 0;
}
Salida
-param_1
-param_2
-param_3
-param_n
Saludos
Cita de: ThunderCls en 13 Febrero 2019, 20:02 PM
EDIT: Disculpa no habia chequeado tu code :silbar: en fin
#include <iostream>
#include <cstring>
#include <vector>
std::vector<char*> cmdparser(char* str, const char *delim)
{
std::vector<char*> str_array;
char *token = strtok(str, delim);
while (token)
{
str_array.push_back(token);
token = strtok(NULL, delim);
}
return str_array;
}
int main()
{
char str[] = "-param_1 -param_2 -param_3 -param_n";
char delim[] = " ";
for(auto s : cmdparser(str, delim))
cout << s << endl;
return 0;
}
Salida
-param_1
-param_2
-param_3
-param_n
Saludos
Gracias, pero lo nececito en C. No es por ser mal agradecido :)
Tratare de portearlo a C.
char* cmdparser(char* str, const char *delim)
{
char* str_array;
char *token = strtok(str, delim);
int i = 0;
while (token)
{
str_array[i++] = token;
token = strtok(NULL, delim);
}
return str_array;
}
Logre portear el codigo, pero no funciona, otra vez :huh:
Cita de: hextakatt en 13 Febrero 2019, 21:02 PM
Gracias, pero lo nececito en C. No es por ser mal agradecido :)
Tratare de portearlo a C.
Igual no tengo nada mejor que hacer ahora mismo :silbar:
char** cmdparser(char* str, const char* delim, int args)
{
char** str_array = (char**)malloc(sizeof(char*) * args);
char* token = strtok(str, delim);
int index = 0;
while (token && index < args)
{
str_array[index] = (char*)malloc(sizeof(char) * strlen(token));
strcpy(str_array[index], token);
token = strtok(NULL, delim);
index++;
}
return str_array;
}
int main()
{
char str[] = "-param_1 -param_2 -param_3 -param_n";
char delim[] = " ";
int args = 4;
char** str_array = cmdparser(str, delim, args);
for(int i = 0; i < args; i++)
cout << str_array[i] << endl;
// es tu responsabilidad
// liberar la memoria que pediste anteriormente
for(int i = 0; i < args; i++)
free(str_array[i]);
free(str_array);
return 0;
}