Disable Console Output from External Program (C++)

2019-08-09 02:18发布

问题:

My code (a simple console application) calls an external program (also a console application). This program spits out many things to the console, which significantly increases runtime when running in batch mode with 10,000+ iterations.

I've tried several ways to disable the output:

  1. Redirecting cout and cerr to fout -> obviously works on disabling couts in the current/local program, but doesn't disable anything from an external .exe.

  2. Some sources have suggested system("cls"), but all that does is clear the screen after the text has been displayed. This adds even more time.

  3. Another suggestion was to hide the console window, but the program still writes to the hidden console, so the entire process still takes more or less the same time.

It seems the only way left is to dig inside the external program's source code and disable all the write commands or redirect it to dev/null or something.

Is there a way to disable a program's write to console commands without having to alter the source code and recompile?

Many thanks!

回答1:

system( "theExternalProgram >nul 2>nul" );