gpt4 book ai didi

php - 将输出从 C++ 传递到 PHP

转载 作者:搜寻专家 更新时间:2023-10-31 01:11:04 24 4
gpt4 key购买 nike

我正在创建一个 PHP 文件以将值传递给 c++ .exe,然后它将计算输出并返回该输出。但是,我似乎无法将 .exe 的输出返回到 PHP 文件中。

PHP 代码:

$path = 'C:enter code here\Users\sumit.exe';
$handle = popen($path,'w');
$write = fwrite($handle,"37");
pclose($handle);

C++代码:

#include "stdafx.h"
#include <iostream>
using namespace std;

// Declaation of Input Variables:
int main()
{
int num;
cin>> num;

std::cout<<num+5;
return 0;
}

最佳答案

我既不建议system也不建议popen,而是建议proc_open 命令:php.net

这样称呼

$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w") // stderr, also a pipe the child will write to
);
proc_open('C:enter code here\Users\sumit.exe', $descriptorspec, $pipes);

之后,您将在 $pipes 中填充句柄,以将数据发送到程序 ([0]) 并从程序接收数据 ([1] )。此外,您还将拥有 [2],您可以使用它从程序中获取 stderr(或者如果您不使用 stderr,则直接关闭)。

不要忘记使用 proc_close() 关闭进程句柄,使用 fclose() 关闭管道句柄。请注意,在您关闭 $pipes[0] 句柄或写入一些空白字符之前,您的程序将不知道输出已完成。我建议关闭管道。

system()popen() 中使用命令行参数是有效的,但如果您打算发送大量数据和/或原始数据,您将遇到命令行长度限制和转义特殊字符的问题。

关于php - 将输出从 C++ 传递到 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15341204/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com