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

前端 未结 5 1108
自闭症患者
自闭症患者 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 21:37

    It sounds like you're coming from a Windows environment, so this might not be the answer you are looking for, but from the command line you can use the pipe redirection operator '|' to redirect the stdout of one program to the stdin of another. http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true

    You're probably better off working in a bash shell, which you can get on Windows with cygwin http://cygwin.com/

    Also, your example looks like a mix of C++ and C, and the declaration of main isn't exactly an accepted standard for either.

提交回复
热议问题