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

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

ysap
1#
ysap Published in 2011-05-05 20:07:57Z
 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 command 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.
Vik David
2#
Vik David Reply to 2011-05-05 20:57:17Z
 Try redirecting both stdout and stderr to the log file. mybat arg2 1>&2> log.txt 
Femi
3#
 Try this: cmd /c "mybat.bat arg2" > log.txt