问题说什么。
最终我想要的是执行 gcc 并在出现错误时捕获输出。问题是错误被写入标准错误而不是标准输出。在 Linux 上我可以
gcc foo.c 2>&1
如何在 Windows 上完成此操作?
问题说什么。
最终我想要的是执行 gcc 并在出现错误时捕获输出。问题是错误被写入标准错误而不是标准输出。在 Linux 上我可以
gcc foo.c 2>&1
如何在 Windows 上完成此操作?
有。只需右键单击控制台窗口,选择Mark
. 用鼠标选择所需区域并单击鼠标右键。现在您可以使用 Ctrl-V 将其粘贴到文本文件中。
如果您需要将程序输出到文本文件中,请像这样运行它:
myprogram.exe > myfile.txt
请参阅此处关于重定向:
1.使用命令重定向运算符
2.从命令提示符重定向错误消息:STDERR/STDOUT
你可以像这样做你想做的事:D:\>dir 1> test.txt 2> testerr.txt
Richard, your "accepted answer" is too long and it is too wrong.
The short answer to your question (as currently stated in your last sentence: "How can I accomplish this on Windows?") is:
But I'll also give you a long answer.
Your 2>&1
redirection works in a cmd.exe
window the same way. I even re-tested it right now, since my cmd.exe experience is a bit rusty. I used this Ghostscript command (intentionally meant to produce output on stdout as well as on stderr):
gswin32c -sDEVICE=nullpage -dFirstPage=12 -dLastPage=11 my-20-page-test.pdf
I got all the expected output into the shell window. Then I did:
gswin32c -sDEVICE=nullpage -dFirstPage=12 -dLastPage=11 my-20-page-test.pdf ^
1>stdout.log
and stderr still printed into the window, but stdout.log had the 'missing' original output. Next I did:
gswin32c -sDEVICE=nullpage -dFirstPage=12 -dLastPage=11 my-20-page-test.pdf ^
2>stderr.log
and stdout now printed into window, while stderr.log had the rest of Ghostscript's messages. Next:
gswin32c -sDEVICE=nullpage -dFirstPage=12 -dLastPage=11 my-20-page-test.pdf ^
1>stdout.log 2>stderr.log
and (as expected): no output in window, all output divided up between stdout.log and stderr.log. Last test:
gswin32c -sDEVICE=nullpage -dFirstPage=12 -dLastPage=11 my-20-page-test.pdf ^
1>all.log 2>&1
and result now:
Which is the same behaviour as stderr/stdout redirection as on Linux.
如果您想要特定命令的输出,有一种将控制台输出推送到文件的简单方法。
这是一个使用 'dir' 命令的简单示例(前导 > 代表您的提示):
>dir > diroutput.txt