4

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?

2
  • 2
    You really need to read up on the basics of piping than asking here. Commented Nov 17, 2015 at 4:45
  • Using xargs will delete the question! Commented Nov 17, 2015 at 4:49

2 Answers 2

3
$./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
Sign up to request clarification or add additional context in comments.

2 Comments

Yup, i was going to update this as the answer. I needed a sequential execution of commands i.e. one after another rather than piping the op of one program to another. Thanks
Interesting . After 25 years of C and C++ I only just discovered that one can send the contents of a file(exampleText.txt) using a C++ cout stream operator, like : cout << "exampleTest.txt" ; and have it piped in to another programs' std::cin! Fascinating!
2

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.

1 Comment

@G Fetterman : Is it possible to reroute Ofstream(disk file) writing code to send theminstead, via a pipe, to another program (on Linux). so instead of file << "line of data" (where file is declared as ofstream file), is it possible to reroute that output to another program, from within C++, and using the PIPE (|) operator on Linux, as such: ./send_prog | ./recv_prog

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.