F.J Kong's Blog

BRAVO HOTEL ONE SIERRA CHARLIE WHISKEY

memory buffer as FILE*

http://stackoverflow.com/questions/539537/memory-buffer-as-file
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
FILE *f = fopen(“/dev/null”, “w”);
int i;
int written = 0;
char *buf = malloc(100000);
setbuffer(f, buf, 100000);
for (i = 0; i < 1000; i++)
{
written += fprintf(f, “Number %d\n”, i);
}
for (i = 0; i < written; i++) {
printf(“%c”, buf[i]);
}
}

Leave a Reply