On Thu, 2004-01-22 at 14:20, Michael Ryan Byrd wrote:
> So, in C, is there a way to capture the stdout from a function?
> 
> I have FunctionX which printfs a bunch of stuff. I'd like to capture that stuff
> into a string without it printing and do a bunch of modifications to it before
> printing.
> 
> Did that make sense? Any ideas how I can do that? 

Usually when you want to capture stdout, it's because you're forking an
external program and interacting with it.

Do you really mean that you want to capture stdout from a function? The
"system call" you showed wasn't a system call, it was forking. Wouldn't
it be easier to just modify the function so it returns a char *?

Assuming you're interfacing with a proprietary library, here's what
you're going to need to do:

Create a pipe with [surprise!] pipe().
Turn that into a FILE * using fdopen.
Assign to stdout before calling the function.
Reset stdout after returning from the function.
Read from the other end of the pipe.

Be sure to use non-blocking IO. I haven't tested this, but I've done
similar and in theory it should work. I'm not going to give more detail
than this because if you're going to do it, you need to have a good
understanding of pipes and such. "Advanced Linux Programming" is good
and free. Google for it.

-- 
Stuart Jansen <[EMAIL PROTECTED], AIM:StuartMJansen>

âThe programmer, like the poet, works only slightly removed from pure
thought-stuff. He builds his castles in the air, from air, creating by
exertion of the imagination. Few media of creation are so flexible, so
easy to polish and rework, so readily capable of realizing grand
conceptual structures.â       -- Fredrick Brooks, Mythical Man Month

Attachment: signature.asc
Description: This is a digitally signed message part

____________________
BYU Unix Users Group 
http://uug.byu.edu/ 
___________________________________________________________________
List Info: http://uug.byu.edu/cgi-bin/mailman/listinfo/uug-list

Reply via email to