【发布时间】:2014-04-20 08:14:07
【问题描述】:
参考,看这个小程序的源代码,EndPointController.exe: http://www.daveamenta.com/2011-05/programmatically-or-command-line-change-the-default-sound-playback-device-in-windows-7/
基本上,它是一个 Visual Studio C++ 程序,它使用 printf 函数将信息写入命令外壳窗口。
这是我在 Windows 7 x64 上运行程序的示例(使用上面链接中提供的编译二进制文件):
C:\Users\James\Desktop>EndPointController.exe
Audio Device 0: Speakers (High Definition Audio Device)
Audio Device 1: AMD HDMI Output (AMD High Definition Audio Device)
Audio Device 2: Digital Audio (S/PDIF) (High Definition Audio Device)
Audio Device 3: Digital Audio (S/PDIF) (High Definition Audio Device)
C:\Users\James\Desktop>
这非常有效。现在,我将尝试将输出重定向到文件:
C:\Users\James\Desktop>EndPointController.exe > test.txt
C:\Users\James\Desktop>type test.txt
C:\Users\James\Desktop>
没用; test.txt 为空。是权限问题吗?
C:\Users\James\Desktop>dir > test.txt
C:\Users\James\Desktop>type test.txt
Volume in drive C has no label.
Volume Serial Number is 16EC-AE63
Directory of C:\Users\James\Desktop
04/20/2014 03:11 AM <DIR> .
04/20/2014 03:11 AM <DIR> ..
05/31/2011 06:16 PM 7,168 EndPointController.exe
04/20/2014 03:12 AM 0 test.txt
2 File(s) 7,168 bytes
3 Dir(s) 171,347,292,160 bytes free
C:\Users\James\Desktop>
不,这似乎不是权限问题。谁能解释这个printf 函数是如何绕过标准输出重定向过程的?
【问题讨论】:
-
我的猜测是运行时库没有正常退出,虽然我不知道为什么。无论如何,如果这是问题所在,您可以通过显式刷新标准输出缓冲区或关闭缓冲来解决它。见stackoverflow.com/a/1716621/886887