Say I have an .exe
, lets say sum.exe
. Now say the code for sum.exe
is
void main ()
{
int a,b;
scanf ("%d%d", &a, &b);
printf ("%d", a+b);
}
I wanted to know how I could run this program from another c/c++ program and pass input via stdin like they do in online compiler sites like ideone where I type the code in and provide the stdin data in a textbox and that data is accepted by the program using scanf or cin. Also, I wanted to know if there was any way to read the output of this program from the original program that started it.
In C on platforms whose name end with X (i.e. not Windows), the key components are:
pipe
- Returns a pair of file descriptors, so that what's written to one can be read from the other.
fork
- Forks the process to two, both keep running the same code.
dup2
- Renumbers file descriptors. With this, you can take one end of a pipe and turn it into stdin or stdout.
exec
- Stop running the current program, start running another, in the same process.
Combine them all, and you can get what you asked for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With