开发者

Redirecting a .bat-file-containing-executable's stdout to a file

I have a batch file that calls an executable program. The program (compiled C code) generates some output to stdout. The batch echos some output as well. When running the bat, I use redirection (>) to get the text to a file.

mybat.bat contains:

myprog.exe arg1 %1 arg3  
echo Done

then, at the console:

C:\> mybat arg2 > log.txt

The problem is that in log.txt I get only the output of the echo Done comman开发者_如何学God and not the output of myprog.exe. Without the redirection, I get the expected output on the screen.

Note: Under Windows XP

Update: this gets even weirder. When running myprog.exe from the command prompt, I get the expected output to the console. Then, when redirecting its output to log.txt, the file is empty! The printing is done using fprintf(stdout, "...") or fprintf(ofp, "...") where ofp is assigned: FILE *ofp = stdout;.

Further investigation: it seems like the fprintf(stdout... lines where redirected, while the fprintf(ofp... are not (yes, the pointer is assigned correctly). I also found that the program crashes at somepoint (at a call to feof()). So, my conclusion is that due to the abnormal termination of the program, the standard output buffers were not written to the file. HOWEVER - this happened only for the lines that use the pointer. I guess that these lines have shorter output, so the flush frequency is lower (I used the stdout line to print a deliberate help message).

Once solving the crash problem, the data is now redirected to the log file. Thanks for your help.


Try redirecting both stdout and stderr to the log file.

mybat arg2 1>&2> log.txt


Try this:

cmd /c "mybat.bat arg2" > log.txt
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜