How do i run a program from another program and pass data to it via stdin in c or c++?

前端 未结 5 1062
自闭症患者
自闭症患者 2021-01-12 20:58

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);
}
         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-12 22:02

    In C on platforms whose name end with X (i.e. not Windows), the key components are:

    1. pipe - Returns a pair of file descriptors, so that what's written to one can be read from the other.

    2. fork - Forks the process to two, both keep running the same code.

    3. dup2 - Renumbers file descriptors. With this, you can take one end of a pipe and turn it into stdin or stdout.

    4. exec - Stop running the current program, start running another, in the same process.

    Combine them all, and you can get what you asked for.

提交回复
热议问题