I'm trying to print some data that I received as a string
and for that I'm using the following function (FechaProg
definition after the function)
uint8_t Extrae_Data(string trama, FechaProg* destino ,char sep) {
uint8_t contador = 0;
string lectura;
stringstream cadena_leida(trama);
Serial.print("La trama es: ");
Serial.println(trama.c_str());
Serial.print("El stringstream es: ");
Serial.println(cadena_leida.str().c_str());
while(getline(cadena_leida, lectura, sep))
{
if(sep == '-')
{
switch(contador)
{
case 1:
{
destino->mes = lectura.c_str();
}
break;
case 2:
{
destino->dia = lectura.c_str();
}
break;
case 3:
{
destino->ano = lectura.c_str();
}
}
}
else if(sep == ':')
{
switch(contador)
{
case 1:
{
destino->hora = lectura.c_str();
}
break;
case 2:
{
destino->minuto = lectura.c_str();
}
break;
}
}
contador++;
Serial.println(contador);
if(contador == 3)
{
//Serial.print("Lectura: ");
Serial.print("La cadena es: ");
Serial.println(destino->mes);
Serial.println(destino->dia);
Serial.println(destino->ano);
}
if(contador == 2)
{
//Serial.print("Lectura: ");
Serial.print("La cadena es: ");
Serial.println(destino->hora);
Serial.println(destino->minuto);
}
}
return contador;
}
The Fechaprog
type that I'm using as a pointer is a struct that looks like follows
struct FechaProg {
const char* dia;
const char* mes;
const char* ano;
const char* hora;
const char* minuto;
const char* segundo;
};
Of course I'm instantiating the object of the type FechaProg
like follows
FechaProg tiempo = {"0"};
and the function is called like follows
Extrae_Data(jsonObj["fecha"], &tiempo, '-')
where jsonObj["fecha"]
is the data that is received.
If trama
and cadena_leida.str().c_str()
are printed before storing the information in the pointer the both are well printed but if I try to print the values stored in the pointers with Serial.println(destino->minuto)
then not readeable information is printed.
Currently I couldn't solve this problem.
UPDATE 01/04/2023: My input arguments for the Extrae_Data
function are a string, a pointer to a struct and a separator.
Inside the function I have a counter which serves to me to store each token in an specific member of the struct once each token is founded; for example, the micro receives the date 2023:01:01
where the separator is ":", then I want the tokens stored like follows:
destino->ano = 2023;
destino->mes = 01;
destino->dia = 01;
This must be done after the getline
function extracts the token from the string, this is to make other operation outside the function but is apparently the assigning of the obtained token to the pointer of the member of the struct what I'm doing wrong.
I repeat, I'm not treating the string as a struct but I want to store the token extracted from the string in the members of the struct.
So, how can I print this?
Thanks in advance for the help.
This is not a complete answer to your problem, as your question is only partially answerable. The Extrae_Data()
function doesn't make a lot of sense and needs more explanation or provide addition context on how it is used in your code.
This only answer how to parse a string into a struct based on your updated comment 01/04/2023.
If you have a piece of data as a string 2023:01:01
, and you want to parse it and assign to different elements of a struct, you can use the good old strtok() function to achieve that:
struct FechaProg {
const char* dia;
const char* mes;
const char* ano;
const char* hora;
const char* minuto;
const char* segundo;
};
// please noted this is not your original function signature
void Extrae_Data(char* trama, FechaProg* destino ,const char* sep) {
destino->ano = strtok(trama, sep);
destino->mes = strtok(NULL, sep);
destino->dia = strtok(NULL, sep);
}
void setup() {
Serial.begin(115200);
char str[]="2023:01:01";
const char* separator = ":";
FechaProg myStruct{};
Extrae_Data(str, &myStruct, separator);
//since you are using esp32, printf is available
Serial.printf("%s %s %s\n", myStruct.ano, myStruct.mes, myStruct.dia);
}