作为引用,请参见此小程序的源代码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
函数将信息写入命令 shell 窗口。
这是我在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
函数如何以某种方式绕过标准输出重定向过程? 最佳答案
程序由于某种原因退出时,似乎没有刷新输出缓冲区。
就在fflush(stdout);
行之前添加return hr;
为我修复了它。
我尝试了其他一些操作,例如使用wprintf将宽字符串转换为窄并传递给printf,并编译为多字节并将字符串转换为窄以传递给printf,但仅手动刷新缓冲区即可。
关于c++ - 为什么此Windows命令行程序无法将其标准重定向到文件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23179931/