1

I am making a website and PHP isn't poerfull enough for me, so i decided to use C++ also. I have made EXE file that do what is supposed to do, but I don't know how I can get the output in php, I cout the output and I thought that would work, so what am I doing wrong.

PHP:

exec(str_replace('\classes', '', dirname(__FILE__)) . '\data\vedur ', $row);

C++:

cout << data;
5
  • What does, in this context, "not powerful enough" mean? I have written quite a bit of PHP code, and unless you are doing something real complicated, I doubt very much that starting a C++ program to solve a problem will be faster than just doing the same thing in PHP. Commented May 17, 2015 at 17:48
  • Powerful enough is maybe not the right word, I just need some instruction how I can send value from C++ to php. Commented May 17, 2015 at 17:49
  • Use php's version of popen? Commented May 17, 2015 at 17:51
  • Yes, I am using one of the newest version Commented May 17, 2015 at 17:54
  • And the function popen doesn't do what you want, or? Commented May 17, 2015 at 18:13

1 Answer 1

1

What you did should work (as long as you print $row).
But that may not be the most efficient as it executes the command and builds an array (so the data is not available until the executable finishes).

What would be better is to use popen(). This executes the command but provides the output of the executable as a file stream (to the rest of your code it looks like an open file). So you can just read the stream like you would read a file.

$handle = popen(str_replace('\classes', '', dirname(__FILE__)) . '\data\vedur ');

if ($handle) {
    while (($line = fgets($handle)) !== false) {
        // process the line read.
    }

    pclose($handle);
} else {
    // error opening the file.
} 

Note: My PHP foo is not strong. So make appropriate checks.

Another note: I assume you are calling the C++ from PHP because you are using the PHP to generate all the correct headers etc.

But if you can do that from the C++ you can potentially call the C++ directly from your web server without using the PHP layer.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.