in C
i want to redirect the output of a process from stdout to write to a "shared memory segment" which can be thought of as a char array or a string with a pointer
i know that there is dup2 but it takes file discriptors as argument not a pointer to an array. is there any way to redirect it to a string?
char string[SIZE];
freopen("/dev/null", "a", stdout);
setbuf(stdout, string);
see freopen and setbuf for their definitions
This should work on UNIX systems:
// set buffer size, SIZE
SIZE = 255;
char buffer[SIZE];
freopen("/dev/null", "a", stdout);
setbuf(stdout, buffer);
printf("This will be stored in the buffer");
freopen ("/dev/tty", "a", stdout);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With