I hava a C function like this ..
func(uint8_t *key,uint8_t keylen) {
FILE *fk;
fk=fopen("akey","wb");
fwrite((char *)key,keylen,1,fk);
puts((char *)key); // for testing
fclose(fk);
sprintf(sstring,"... \"%s\" ... ",(char *)key);
// ...other irrelevant stuff for here
system(sstring);
}
I want the key provided as input to be used at the
place marked \"%s\" in sprintf command
Input :
Key=qwerty
Output :
$cat akey
qwerty
and output of puts((char *)key) is –
qwerty�
// include newline character as well
Suggest me on this , I tried with this (at their respective position in the code)
but getting segmentation fault
char *p;
memcpy(p,(char *)key,keylen);
puts((char *)p);
You could use the form
%.*sto print it out:Here an extract from the Wikipedia entry on
printf:NOTE: Using an 8-bit type (
uint8_t) to represent the length limits the length of the string to 255 characters. Better is to usesize_t.