0

The codes for the two programs are,

p1.c:

#include <stdio.h>

int main () {
    printf("Program1");
    return 0;
}

p2.c:

#include <stdio.h>

int main (char argc, char *argv[]) {    
    printf("%s", argv[1]);
    printf(" | Program2");
    return 0;
}

When p1 | p2 is entered in the CMD, the expected output is: Program1 | Program2. But the output I get is: (null) | Program 2. Clearly the output of the p1 is not taken in from p2. How can I resolve this issue?

sope
  • 86
  • 2
  • 9

1 Answers1

1

You don't understand how piping works. To pipe something into something else means "use the standard output of program one and provide it to the standard input of program 2." By using argv, you are assuming that the output of program 1 is going to the arguments of program 2 and that isn't correct. You need to scanf (or some equivalent) stdin in order to get the result you are looking for.

Either that or you need to invoke p1 in such a way that its output does get fed in as an argument.

keefer
  • 385
  • 2
  • 7
  • That worked! By the way how can I invoke p1 in a way that its output goes in to p2 as an argument? – sope Oct 05 '14 at 03:20
  • | is a CMD exe character. It means nothing to any program other than CMD.EXE. If using | then CMD.exe needs to interpret it. I refer to it's use in p2.c. – Noodles Oct 05 '14 at 09:02
  • Per http://stackoverflow.com/questions/2768608/batch-equivalent-of-bash-backticks I would suggest `for /f %a in ('p1') do p2 %a`. If in a batch file, remember to use %% instead of %. – keefer Oct 05 '14 at 16:43