I’d like to define two functions, fdump and sdump, to dump a struct to a file or to a buffer using fprintf and sprintf in each case.
Is there a way to define them without repeating the code in the two functions?
One solution could be define sdump and then fdump based on it, e.i.:
void fdump(FILE* f, struct mystruct* param) {
char buffer[MAX];
sdump(buffer, MAX, param);
fprint(f, "%s", buffer);
}
But that solution wastes and intermediate buffer. Although maybe fprintf does the same. Other solution could be by means of preprocessing macros but it looks quite complicated. Please, any ideas?
Thanks in advance
You can use
fmemopento give you a file handle that points to a chunk of memory and then write just one version of your function that takes a file handle: