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

前端 未结 5 1103
自闭症患者
自闭症患者 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:52

    This is my solution and it worked:

    sum.cpp

    #include "stdio.h"
    int main (){
      int a,b;
      scanf ("%d%d", &a, &b);
      printf ("%d", a+b);
      return 0;
    }
    

    test.cpp

    #include 
    #include 
    
    int main(){
      system("./sum.exe < data.txt");
      return 0;
    }
    

    data.txt

    3 4
    

    Try this solution :)

提交回复
热议问题