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?