将包含可执行文件的 .bat 文件的标准输出重定向到文件
我有一个调用可执行程序的批处理文件。该程序(编译的 C 代码)生成一些输出到 stdout。该批处理也回显了一些输出。运行 bat 时,我使用重定向 (>) 将文本获取到文件。
mybat.bat 包含:
myprog.exe arg1 %1 arg3
echo Done
然后,在控制台:
C:\> mybat arg2 > > log.txt
问题是在 log.txt
中,我只得到 echo Done
命令的输出,而不是 myprog.exe 的输出
。如果没有重定向,我会在屏幕上得到预期的输出。
注意:在 Windows XP
更新下:这会变得更加奇怪。从命令提示符运行 myprog.exe 时,我在控制台上得到了预期的输出。然后,当将其输出重定向到 log.txt
时,该文件是空的!打印是使用 fprintf(stdout, "...")
或 fprintf(ofp, "...")
完成的,其中 ofp
是分配:FILE *ofp = stdout;
。
进一步调查:似乎 fprintf(stdout...
行已重定向,而 fprintf(ofp...
) 则未重定向(是的,指针已正确分配)我还发现程序在某个时刻崩溃(在调用 feof()
时)。因此,我的结论是,由于程序异常终止,标准输出缓冲区未被写入。然而 - 这仅发生在文件中。 频率较低(我使用 stdout 行故意打印帮助消息)。
使用指针的行我猜想这些行的输出较短,因此刷新 感谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将 stdout 和 stderr 重定向到日志文件。
Try redirecting both stdout and stderr to the log file.
试试这个:
Try this: