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?
posix 2008 標準には、open_memstream () という非常に優れた関数があります。次のように使用します。
char* buffer = NULL;
size_t bufferSize = 0;
FILE* myStream = open_memstream(&buffer, &bufferSize);
fprintf(myStream, "You can output anything to myStream, just as you can with stdout.\n");
myComplexPrintFunction(myStream); //Append something of completely unknown size.
fclose(myStream); //This will set buffer and bufferSize.
printf("I can do anything with the resulting string now. It is: \"%s\"\n", buffer);
free(buffer);