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

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

user1783 Published in June 23, 2018, 7:52 am

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.317683 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO