Reputation: 1548
For reference, see the source code for this small program, EndPointController.exe: http://www.daveamenta.com/2011-05/programmatically-or-command-line-change-the-default-sound-playback-device-in-windows-7/
Basically, it is a Visual Studio C++ program that is using a printf
function to write information to a command shell window.
Here's an example of me running the program on Windows 7 x64 (using the provided compiled binary from the above link):
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>
This works perfectly. Now, I'll try to redirect the output to a file:
C:\Users\James\Desktop>EndPointController.exe > test.txt
C:\Users\James\Desktop>type test.txt
C:\Users\James\Desktop>
It didn't work; test.txt
is empty. Is it a permissions issue?
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>
No, it does not seem to be a permissions issue. Can anyone explain how this printf
function is somehow circumventing the standard out redirection process?
Upvotes: 2
Views: 1135
Reputation: 93476
I have tried downloaded the project files from the link you included and run the executable that is already built and included in the Release folder and it works as expected. I also re-built the code in VC++2013 and that too works as expected.
I suspect either operator error or some system issue - the information in your question does not seem to suggest operator error however; you have provided evidence that this is not teh case.
I ran the code from C:\Users\<userprofile>\Documents\Visual Studio 2013\test\Release
. Desktop
is a "special" folder in Windows which may have some bearing, though I doubt it. Either way, I don't think it is a programming issue.
Upvotes: 1
Reputation: 4924
It appears that the output buffer isn't being flushed when the program exits for some reason.
Adding fflush(stdout);
right before the return hr;
line fixes it for me.
I tried a few other things, like converting the wide string to narrow and passing that to printf, using wprintf, and compiling as multibyte and converting the string to narrow to pass to printf but only manually flushing the buffer worked.
Upvotes: 3