I have the following sscanf statement:
sscanf(line, "%*s %511s %511s",protocol,hexdata)
Now line consists of the following form " a b c ". It's clear I am ignoring the value a by giving %*s. I take the values of b and c into protocol and data.
What I would like to do is I dont want to hardcode 511. I am just unable to do so. I tried the following but it does not seem to work.
sprintf(log_buffer,"1234 56789");
printf("\n Buffer is : %s \n",log_buffer);
strcpy(format,"%*s "); // gives %*s
sprintf(format1, "%%%ds", 5); // gives %5s
printf("\n Format is : %s ",format);
printf("\n Format1 is : %s ",format1);
strcat(format,format1);
printf("\n new format is : %s ",format);
sscanf(log_buffer,format,name);
printf(" Name is : %s ",name);
printf(" \n Size of name %d ",strlen(name));
This gives me junk value.
Sample output:
Buffer is : 1234 56789
Format is : %*s
Format1 is : %5s
new format is : %*s %5s
Name is : �
8r�2�%*s %5s
Size of name 15
What is wrong here ? I just have to prevent hardcoding variable field.
my declarations are as follows char format[5]={0};
The format string can hold 4 chracters and the zero terminator. That is not enough for "%*s %5s"!
Increase the array size.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With