print2fp(const void *buffer, size_t size, FILE *stream) {
if(fwrite(buffer, 1, size, stream) != size)
return -1;
return 0;
}
How to write the data into string stream instead of File stream?
print2fp(const void *buffer, size_t size, FILE *stream) {
if(fwrite(buffer, 1, size, stream) != size)
return -1;
return 0;
}
How to write the data into string stream instead of File stream?
simply use sprintf http://www.cplusplus.com/reference/cstdio/sprintf/
example from the refference:
Output:
[5 plus 3 is 8] is a string 13 chars long
Update: Based on recommendations in comments: Use snprinft as it is more secure (prevents buffer overflow attacks) and is portable.
Notice that snprintf seconds argument is actually the max allowed size to use, so you can put it to a lower value than sizeOfBuffer, however for your case it would be unnecessary. Snprintf only writes SizeOfBuffer -1 chars and uses the last byte for the termination character.
And just to piss off everyone from the embbed and security department, here is a link to http://www.cplusplus.com/reference/cstdio/snprintf/
There is a very neat function in the posix 2008 standard: open_memstream(). You use it like this: