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

前端 未结 5 1063
自闭症患者
自闭症患者 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.

    0 讨论(0)
  • 2021-01-12 21:40

    The easiest way I know for doing this is by using the popen() function. It works in Windows and UNIX. On the other way, popen() only allows unidirectional communication.

    For example, to pass information to sum.exe (although you won't be able to read back the result), you can do this:

    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        FILE *f;
    
        f = popen ("sum.exe", "w");
        if (!f)
        {
            perror ("popen");
            exit(1);
        }
    
        printf ("Sending 3 and 4 to sum.exe...\n");
        fprintf (f, "%d\n%d\n", 3, 4);
    
        pclose (f);
        return 0;
    }
    
    0 讨论(0)
  • 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 <stdio.h>
    #include <stdlib.h>
    
    int main(){
      system("./sum.exe < data.txt");
      return 0;
    }
    

    data.txt

    3 4
    

    Try this solution :)

    0 讨论(0)
  • 2021-01-12 21:57

    How to do so is platform dependent.

    Under windows, Use CreatePipe and CreateProcess. You can find example from MSDN : http://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx

    Under Linux/Unix, you can use dup() / dup2()

    One simple way to do so is to use a Terminal (like command prompt in windows) and use | to redirect input/output.

    Example:

    program1 | program2
    

    This will redirect program1's output to program2's input. To retrieve/input date, you can use temporary files, If you don't want to use temporary files, you will have to use pipe.

    For Windows, (use command prompt):

    program1 <input >output 
    

    For Linux, you can use tee utility, you can find detail instruction by typing man tee in linux terminal

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题