I have two programs right now..I'd like to take the output of one program, and use it as the input for the other program. At this time, the two programs are completely separate from each other. I would rather not merge the two because one is written in C and the other is written in C++, and each of them are relatively large. What would be the easiest way to pass information from my C++ program to my C program? The idea seems easy enough I just don't know of an easy way to do it.
Option 1: Use a pipe. On the shell, when you run your programs, execute the command:
$ program1 [arguments] | program 2 [argumens]
The standard output of program1 will be redirected to the standard input of program2. This works on Linux and Windows (don't know about other platforms, but I think it's an universal technique).
The character between the two programs is a pipe symbol, which I think is located between 'left shift' and 'Z' on all keyboards.
Option 2: Write program1 output to a file and read it on program2. Not as elegant as the above, but works too.
Also, using pipes you probably increase performance, since both programs are executed simultaneously, and each time program1 writes output, program2 reads and processes it. Using files, program2 has to wait for program1 to execute completely, then process its output.
@bbgst the use of pipes sounds like it will work for my needs. Basically, my first program will output different characters depending on an external input, and depending on which character is output from program 1, program 2 will read it and act accordingly.
I tried your method without listing any arguments and it didn't work quite right. My first program is supposed to have a visual interface, which didn't start up. My relevant code for program 1 is this:
This code is written in c++, and my code for program 2 written in C looks like this:
while ((exit != 'y'))
{
a=getchar();
and then a line of if statements depending on what "a" is. I know both programs work separately, but I'm having trouble getting the pipe method to work even though it describes exactly what I need. Any suggestions?
I'm not very experienced in programming, so in my opinion these are relatively large programs at about 5 .cpp files for program 1 and the same .c files for program 2. I'm just not understanding why my first program isn't running. I typed the entire path to the executable, and it didn't bring up the visual interface like normal. I don't think this is a problem of size.
I tried a few things, and when I just type the path to the executable in terminal (i'm running ubuntu 10.10), I receive the following error:
Error initializing: File not found!
I think this is a big problem because this is the way the executable is run in the pipe method.
When I change my directory to the directory of the program them do a ./program, it works fine.