C ++显示输出到php

I have a C++ console application that returns some kind of things, then, I need to show that in a browser. So I tried to write every console.write from C++ to a .txt file for future PHP reading, but no success because it doesn't write the errors!

Then I tried to use exec('program.exe', $output); at PHP but no success too.. :/

  • Is there another way to do that?
  • Or any function in C++ that writes to a .txt every single thing that shows up in the console?
  • Or some function in another Programming Language that catches everything in the console?

There are three standard file handles known as stdin, stdout, stderr. Looks like you're only fetching stdout right now. Try to redirect stderr to stdout.

exec('program.exe 2>&1', $output);

(works not only on *nix but also win32).

see also: In the shell, what does " 2>&1 " mean?

You have to understand that cout (standard output) and cerr (standard error output) are two different streams, so probably you forgot to redirect cerr to your text file as well. Here some ideas you can use:

  • Redirect your streams to a file on your code (see How to redirect cin and cout to files?)
  • Redirect the output of any console program from the command line (you don't require to change the code and works on windows and unix / linux) by calling it this way executable.exe > out.txt 2>&1 (see Redirect all output to file)
  • Connect your web application to your console application using TCP sockets (might be an overhead but useful if the application requires some kind of interactive control)