I have a pile of batch files that do something like this with program A:
A > log.txt ; grep 'result:' log.txt > result.txt || raise_hell.sh
What I’m trying to do is modify A so that it’ll print a bunch of intermediate results as it goes. However, it’s not as easy as you might think because A does the following:
- calls _freopen(“somefile”,”w”,stdout), and then
- calls printf (and variants thereof) in a million places and expects the output to end up in somefile.
It’s not really practical for me to change every single printf in the project. Nor is it practical to change every batch file, so I don’t really know where my output’s being redirected to.
Ideally, I’d like to have some way to get to the “original value” of stdout, and just write there. But how? I can even replace the call to freopen with something else so long as the output still ends up where it should.
In other words I’d like for printf to end up in a file, and to have some other way to print stuff to standard out.
I’ve tried a number of things, in part based on answers to variations of this question. However, none of those work because they still all end up writing to somefile. Help would be welcome.
edit: Just to be clear, this is on Windows, using Visual Studio 2010.
OK, I’m not a Windows guy, but just as an answer to the specific question: how to mess w/stdout. In UNIX we’d use dup/dup2, which according to:
http://www.suacommunity.com/dictionary/dup-entry.php
is only slightly different in windows. The basic idea in UNIX is that stdout is file descriptor 1…changing what is open there is equivalent to redirecting it. Using dup, you can make a copy of it first, in case you want to put it back.
In general, though, I’d use stderr for progress messages…that is why it exists.