5

I am trying to pass the output generated by one executable as input into another. I have been able to send in one line at a time.

The problem is when I try to send in a 'sequence of lines generated in a while loop' from Program1 to be read as input by Program2. I tried piping the executables in terminal (as given below), but it fails to work.

./Program1 | ./Program2  
./Program1 |xargs ./Program2  
./Program1 > ./Program2  

I want to avoid File I/O.

Note: Platform : Linux

==================

Something along the lines of the following example

Program1 (Writing to Terminal)

int main(int argc, char *argv[])
{
    int i = 2200;
    while(1){    
        printf("%d \n", i);
        i++;
    }
}

Program2 (Reading from Terminal, the output of Program1)

int main(int argc, char *argv[])
    {   
        while(1){ 
        // Read 'i' values
        cout << "There are " << argc << " arguments:" << endl;
        // Loop through each argument and print its number and value
        for (int nArg=0; nArg < argc; nArg++)
        cout << nArg << " " << argv[nArg] << endl;
        }

        return 0;
    }
mystique
  • 507
  • 2
  • 9
  • 22
  • You can't 'pass' continuous stream as a set of arguments to another process as the list of arguments has to be known **before** process creation – Serge Jun 03 '16 at 01:45
  • I see nothing in the second program that reads from the terminal. – Sam Varshavchik Jun 03 '16 at 01:46
  • 1
    also, the last form you used just overrides the content of `./Program2` with an output generated by `./Program1` – Serge Jun 03 '16 at 01:47
  • 5
    when you "pipe" two programs in the shell, the standard output (`stdout`) of the first program will be redirect to the standard input (`stdin`) of the second program and not to the command line arguments. So on you case you just need to do ``` int i ; while (true) { cin>> i; cout<< i; } ``` – André Oriani Jun 03 '16 at 01:53

1 Answers1

5

The problem is that you are trying to read the program arguments. But when you pipe from one program to the next the output from the first program becomes the standard input (std::cin) of the second program.

Try this for program 2:

#include <string>
#include <iostream>

int main()
{
    std::string line;

    while(std::getline(std::cin, line)) // read from std::cin
    {
        // show that it arrived
        std::cout << "Line Received: " << line << '\n';
    }
}
Galik
  • 47,303
  • 4
  • 80
  • 117
  • I have a program which is quite a large command-line calculation program. It outputs a whole bunch of messages to screen. They are not needed in a second program. But, I have the need to send via a pipe , data that is currently being written using a normal *ofstream* disk file - to another program, on Linux. Do I do it using *cout* , or a call to *std::rdbuf*, to redirect stream buffers? None of them seem to work? and then at the command , how would we use the pipe (|) operator, or >(redirection) operator to receive the data in the second program? ./send_prog | ./recv_prog – Cosmic OM... Apr 29 '20 at 17:48
  • @GreatCentralSun From what I gather in your comment your first program is writing to a file. In that case your second program can simply read from that file using `std::cin` if you redirect the standard input to read from that file on the command line. `recv_prog < disk_file`. – Galik Apr 29 '20 at 18:00
  • Thanks, The folks whose code I'm looking at want the file-writing part replaced with a piped output , that can be read by recv_prog using | operator. I'm rusty with commandline programming, but that should be possible using *cout* or FD(file descriptors)? no? Please enlighten Me. – Cosmic OM... Apr 29 '20 at 18:48
  • Also, one more question: Is it possible to pipe selected data out of send_prog, to recv_prog , using C++(not Linux utils)? Or is it the case that all output to *cout* from send_prog will be send to *cin* of recv_prog? – Cosmic OM... Apr 29 '20 at 19:14
  • 1
    @GreatCentralSun Each program just reads or writes from standard input or output. It is how you invoke them at the command line that pipes one into the other. If you want to pipe the output just write to `std::cout` and if you want to pipe the input just read from `std::cin`. – Galik Apr 29 '20 at 22:52
  • Got it. I thought as much, just wanted to clarify. – Cosmic OM... Apr 30 '20 at 14:52