SeasonalShot
SeasonalShot

Reputation: 2589

Pipe output of a c++ program to another program

main.cpp Takes 2 command line arguments and opens file named "exampleTest.txt" and writes to that file.

#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char const *argv[])
{
    ofstream op;
    op.open("exampleTest.txt",ios::out);
    op<<argv[1]<<"\n"<<argv[2]<<endl;
    op.close();
    return 0;
}

pgm1.cpp OPens the file named "exampleTest.txt" and displays the contents of that file

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char const *argv[])
{
    string line;
    ifstream fs;
    fs.open("exampleTest.txt",ios::in);

    if (fs.is_open())
    {
        while(getline(fs,line))
        {
            cout<<line<<endl;

        }
    }
    fs.close();

    return 0;
}

By compiling the 2 programs and running them individually works just fine. However, i want to pipe the output the main.cpp output to the program pgm1.cpp

What i tried:

g++ main.cpp -o main
g++ pgm1.cpp -o pgm
./pgm |./main abc def

What i expect the output to be:

abc 
def

What i am getting:

<no output>

My reasoning: the 'main' creates the file 'exampleTest.txt' with abc def as the input to the file. While this file is given to the 'pgm' (piped to...), it should be able to read the file contents and output

abc
def

in the terminal, instead i am not getting any output. (Works if i run each of the file separately )
Could you please let me know what am i missing?

Upvotes: 4

Views: 8704

Answers (2)

Ramesh.N
Ramesh.N

Reputation: 81

$./main aaa cccc ;./pgm
aaa
cccc

Since you are writing the output of main program to a file and using the file name (hard coded) int he pgm1.cpp file, using pipe redirection will not be helpful as the pipe usually take the stdin as input.

Slight modification of your program can demonstrate this (One of the method):

main.cpp

#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char const *argv[])
{
    ofstream op;
    op.open("exampleTest.txt",ios::out);
    op<<argv[1]<<"\n"<<argv[2]<<endl;
    op.close();
    cout << "exampleTest.txt";
    return 0;
}

pgm1.cpp

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char const *argv[])
{
    string line;
    ifstream fs;
    fs.open(argv[1],ios::in);

    if (fs.is_open())
    {
        while(getline(fs,line))
        {
            cout<<line<<endl;

        }
    }
    fs.close();

    return 0;
}

output:

  $ ./main ddd eee | xargs ./pgm
    ddd
    eee

Upvotes: 3

G Fetterman
G Fetterman

Reputation: 456

You're writing your outputs to file and then reading them, but pipes (usually) work by taking the output of one program onto stdout and using that as the stdin input for another program.

Probably the simplest solution for you is to have main.cpp write the output filename to stdout after it's done.

EDIT

Another option is to have main.cpp write the lines to stdout as well as to the file, and then have pgm1.cpp read the lines from stdin, instead of the file. Depending on your other requirements, this may be a better solution, or it may not.

Upvotes: 2

Related Questions