Is it possible to get a program output during its operation?

If I have a Windows console program written using C ++, can I get this standard output from the program while the program is running? And if not, what's the best way to rewrite a program? I know that I can output files and constantly check these files for updates. Is there another way? Is there a better way?

+4
source share
5 answers

The Code Project has interesting articles:

+2
source

If this is a ready-made console executable, you can redirect it to a file as follows:

c:> echo Some text> file

or

c:> program> file

If you mean this? Since your question is not entirely clear.

\\ to another program

Oh, ok. But my first answer is also used for this. Since there is another possibility, for example:

c:> program1 | program2

make a call between console programs
program2 gets stdin on it, which program1 throws into stdout
His usual old Unix-way practice in console programs.
And so you don’t need to rewrite programs to support them.

+1
source

Yes, if you run the program yourself:

in CreateProcess , you pass STARTUPINFO where you can specify descriptors for SDIN, STDOUT and STDERR. Note that oyu must be provided with all three as soon as you specify the STARTF_USESTDHANDLES flag.

In addition, the descriptors must be inherited (otherwise the child process will not be able to access them), so SECURITY_ATTRIBUTES should basically look like this:

 SECURITY_ATTRIBUTES secattr = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE }; 

You can open descriptors on disk files containing output and output data. Alternatively, it can be Pipes , which can be read / written gradually while the console application is running.

+1
source

If you are only interested in the stdout program, popen () makes this pretty simple:

 FILE* program_output = popen("command line to start the other program"); //read from program_output as you would read a "normal" file //... pclose(program_output); 
0
source

To do this, you will most likely need to use channels, and since you are using Windows, here is a link to an MSDN article with an example that seems to do exactly what you wanted.

0
source

All Articles