You can replace all printf
s by myprintf
where myprintf
would be a function similar to fprintf
but writing output to a buffer. myprintf
would essentially use vsprintf to get the output into a char buffer and then to your memory buffer.
Here is a very simple and naive implementation of the concept with no error checking just to give you an idea.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
void myprintf(char *mybuffer, const char *format, ...)
{
va_list args;
va_start (args, format);
char tempbuffer[100] ;
vsprintf (tempbuffer, format, args);
strcat(mybuffer, tempbuffer) ;
va_end (args);
}
int main()
{
char buffer[100] ;
buffer[0] = 0 ;
myprintf (buffer, "Hello world!\n") ;
myprintf (buffer, "Hello %s %d", "world", 123) ;
printf (buffer) ;
return 0 ;
}