Search code examples
c++inter-process-communicat

Send data to another C++ program


Is it possible to send data to another C++ program, without being able to modify the other program (since a few people seem to be missing this important restriction)? If so, how would you do it? My current method involves creating a temporary file and starting the other program with the filename as a parameter. The only problem is that this leaves a bunch of temporary files laying around to clean up later, which is not wanted.

Edit: Also, boost is not an option.


Solution

  • Clearly, building a pipe to stdin is the way to go, if the 2nd program supports it. As Fred mentioned in a comment, many programs read stdin if either there is no named file provided, or if - is used as the filename.

    If it must take a filename, and you are using Linux, then try this: create a pipe, and pass /dev/fd/<fd-number> or /proc/self/fd/<fd-number> on the command line.

    By way of example, here is hello-world 2.0:

    #include <string>
    #include <sstream>
    #include <cstdlib>
    #include <cstdio>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    int main () {
    
      int pfd[2];
      int rc;
    
      if( pipe(pfd) < 0 ) {
        perror("pipe");
        return 1;
      }
    
      switch(fork()) {
      case -1: // Error
        perror("fork");
        return 1;
    
      case 0: { // Child
        // Close the writing end of the pipe
        close(pfd[1]);
    
        // Create a filename that refers to reading end of pipe
        std::ostringstream path;
        path << "/proc/self/fd/" << pfd[0];
    
        // Invoke the subject program. "cat" will do nicely.
        execlp("/bin/cat", "cat", path.str().c_str(), (char*)0);
    
        // If we got here, then something went wrong, then execlp failed
        perror("exec");
        return 1;
      }
    
      default: // Parent
        // Close the reading end.
        close(pfd[0]);
    
        // Write to the pipe. Since "cat" is on the other end, expect to
        // see "Hello, world" on your screen.
        if (write(pfd[1], "Hello, world\n", 13) != 13)
          perror("write");
    
        // Signal "cat" that we are done writing
        close(pfd[1]);
    
        // Wait for "cat" to finish its business
        if( wait(0) < 0)
          perror("wait");
    
        // Everything's okay
        return 0;
      }
    }