Remap standard error for string in C

I would like to be able to redirect stderr to string C, because I need to use the string in the program that I am writing. I would like not to write the file first (to the hard drive) and then read the file to get the line. What is the best way to do this?

+4
source share
3 answers

You can simply use setbuf () to change the stderr buffer:

 #include <stdio.h> int main(void) { char buf[BUFSIZ]; setbuf(stderr, buf); fprintf(stderr, "Hello, world!\n"); printf("%s", buf); return 0; } 

prints:

 Hello, world! Hello, world! 

Note: you must change the buffer before any operation in the stream.

+12
source

This suggests that stderr comes from another program. If you want to capture all the output of stderr from your program and process, then in a separate thread, listen to write to stderr via fopen("/dev/stderr", "r") .

+2
source

I gave a solution using C ++ to redirect stdout and stderr to a function (called for each line). I know this was tagged C, but the courage of my solution uses the POSIX api.

See fooobar.com/questions/1462014 / ...

-1
source

All Articles