sending output stream to char[] in c

571 Views Asked by At

I have been using dup and freopen to rerout stdout to a file as below:

fflush(stdout);
fgetpos(stdout, &pos);
fd = dup(fileno(stdout));
freopen("stdout.out", "w", stdout);

What I would like to do would be rerout it to a char[], so that I can manipulate it. obviously this isn't very useful when writing with printf, but when using libraries that write to stdout, it would be helpful to get the output in the code so I can manipulate it, if necessary.

1

There are 1 best solutions below

0
On

Assigning to stdout is not guaranteed to work, but maybe it will work on your platform, otherwise see this answer based on shmem_open, mmap and fmem_open: https://stackoverflow.com/a/25327235/2332068