Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing proc_open() to run C executable in PHP
    primarykey
    data
    text
    <p>I am sorry (again) if you find this question noob. But I am really having a problem on <strong>proc_open</strong>.</p> <p>I have a <strong>C file</strong> and I want to run it using <code>proc_open()</code> and read the input from a textfile. I was able to fetch and feed the input to the executable. The problem is that I just hardcoded the <code>array of fetched strings</code>. </p> <p><strong>PHP code fragments:</strong></p> <pre><code> $descriptorspec = array( 0 =&gt; array("pipe", "r"), 1 =&gt; array("pipe", "w"), 2 =&gt; array("file", "error.log", "a") ); $process = proc_open('C:/xampp/htdocs/ci_user/add2', $descriptorspec, $pipes); sleep(1); if (is_resource($process)) { //Read input.txt by line and store it in an array $input = file('C:/xampp/htdocs/ci_user/input.txt'); //Feed the input (hardcoded) fwrite($pipes[0], "$input[0] $input[1]"); fclose($pipes[0]); while ($s = fgets($pipes[1])) { print $s."&lt;/br&gt;"; flush(); } fclose($pipes[1]); } </code></pre> <p><strong>add2.c</strong></p> <pre><code> #include &lt;stdio.h&gt; int main(void) { int first, second; printf("Enter two integers &gt; "); scanf("%d", &amp;first); scanf("%d", &amp;second); printf("The two numbers are: %d %d\n", first, second); printf("Output: %d\n", first+second); } </code></pre> <p><strong>streams on pipes[1] ( the printf's)</strong></p> <pre><code>Enter two integers &gt; The two numbers are: 8 2 Output: 10 </code></pre> <p><strong>Question:</strong> Is there a "<strong>dynamic way</strong>" on how the "<strong>$input</strong>" elements will be laid out as input on</p> <pre><code> fwrite($pipes[0], "$input[0] $input[1]"); </code></pre> <p>Or is there a more convenient way to get inputs from a file and feed it whenever there is <code>scanf</code> in the C executable that was ran by the <code>proc_open()</code>.</p> <p>(Btw, for those having trouble in <code>proc_open()</code>, especially for the starters like myself, I hope my codes help you somehow. This is my first time to make it run after few attempts so my codes are simple.)</p> <p>And for the Pro's, please help meeeee. :( THANK YOU!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload