alf
alf

Reputation: 681

Writing to the STDIN of a C program from PHP

I literally have almost no PHP experience at all whatsoever. I have a C program that I'd like to put online (it's computationally intense, I'd rather keep it in C), so my question is this: How to I write to the STDIN of a C program using PHP (and read from it's STDOUT)? Like, taking a string from an HTML form, or something similar, and writing that to the STDIN.

Upvotes: 2

Views: 2083

Answers (4)

DaveyBoy
DaveyBoy

Reputation: 2924

If your php program just echoes strings (using echo),

php -f program.php | c_program

Upvotes: 0

Catalin
Catalin

Reputation: 868

Long story short, check this out: http://php.net/manual/en/function.proc-open.php

Upvotes: 2

Mob
Mob

Reputation: 11096

Try using fwrite and fget

fwrite(STDOUT, "\n-CMD$: "); //Output 
$site = fgets(STDIN)   // Get Input

Upvotes: 1

VolkerK
VolkerK

Reputation: 96159

Take a look at the System program execution section of the manual.
Esp. proc_open — Execute a command and open file pointers for input/output

You could also pass the parameter as an argument to the executable and fetch the output which can easily be done via escapeshellarg() and exec()

You might also be interested in http://www.swig.org/

SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages. SWIG is used with different types of target languages including common scripting languages such as Perl, PHP, Python, Tcl and Ruby.

Upvotes: 1

Related Questions