tags:

views:

113

answers:

3

How can I read from the stdout of my program?

One of my threads needs to access the stdout to read what the other threads are logging.

I'm using the dear old plain C.

+2  A: 

You shouldn't. stdout is supposed to be the output to your program and you're not therefore supposed to be reading it in as well. If you want to do this, do your logging elsewhere.

Stephen Cross
I have no control on the logging - if I had, I wouldn't have the problem...
Frankie
Ok, would it be possible for you to redirect stdout to point to a file or stream you created? That would be another solution.
Stephen Cross
+1  A: 

If I understand you correctly, you want your threads to communicate with each other. Using stdout for inter-thread communication doesn't seem to be the right way to solve this problem.

Perhaps you should try a Web search for "communication between threads".

migu
+4  A: 

Make stdout use a pipe by replacing the stdout fd with the write side fd of a pipe using dup.

Martijn
This sounds definitely intersing but I don't know how to implement it. I'm trying something like: int pfd[2]; char buf; pipe( pfd ); dup2( pfd[1], 1 ); while( read( pfd[0], }
Frankie
You can't use printf on the read side of the pipe since you just routed stdout to the write side of the pipe. If you need to print to the console, dup the stdout fd (before you change it) and use that fd with dprintf.
Martijn