stdout redirection

stdout redirection



         I am using a huge 'C' library to be build CFD app. The library contains fortran interfaces, so, I am willing to write a nice GUI in fortran to simulate a chemical process. The problem that arises is this one: The library contains a lot of printf streams and that goes nowhere in windows app. What I want to know is: Is there a way to redirect to a file [ open(6,.....) ?] or to a MessageBox those streams with fortran statements?


5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

How about writing your own function, say my_printf, that processes arguments the same way as printf does but which then redirects output where you want them to go, some file or MessageBox, etc. instead of stdout?

You can then do a global replace using an editor for all your printf statements in code to use my_printf instead.


I couldn't figure out if this is what you had in mind, but Windows cmd shell redirection is much the same as bash.
As previously responded, if you are writing a GUI, it seems you would consider using the facilities of your GUI to control file output.



       One thing that I can't do is change the statement 'printf' on the 'C' libary. And that is because there are too many and the files are hundreds. So, the only way I have is to write some fortran code that grabs those messages from those streams or build a 'C' file to redirect it.I came here to have an insight about the best way of doing it.




I've played with this, but memory fails me for specifics, so rambling follows.  Anyway, you can use the Win32 API to redirect stdout from within a process.  Depending on what the C runtime does (particularly during its initialiation), this may be sufficient.  If not, an alternative is to set the redirection up in a wrapper process that sets the necessary pipes up before the program with the C library is executed.

See post #5 in this thread, written for Fortran.  The example creates a pipe, replaces stdout with the write end of that pipe and then creates a thread to listen to the read end of the pipe and shuttle whatever comes through somewhere else.  This is all done in-process, so runtime initialization in a particular way might defeat it (because by the time stdout is intercepted the runtime may have already queried the relevant handles etc. 

See post #9 in this thread for an example of redirection when creating a child processes, again written in Fortran.  Specifically look at what the check_program_output procedure does.

You may be able to use C library functions to achieve the same - cant' recall whether they are standard or vendor additions that I've played with from time to time.

Leave a Comment

Please sign in to add a comment. Not a member? Join today