How to use pipe to apply a text to a program

14,581

First of all, a pipe connects two processes, not files (including text files), such that the output of one goes to the input of the other. The presumption is that the process "generating" the output sends it to STDOUT, which becomes the source for the pipe, and that the process "receiving" the input reads it from STDIN, which becomes the destination of the pipe. You cannot connect a pipe to a text file, or any other file, only to processes.

Second, when using a pipe the process on the left side of the pipe is the one that uses STDOUT, and the process on the right side of the pipe uses STDIN. Therefore, your attempted command would be trying to send the output of my_program to the pipe, not reading from it.

If you properly presented the instructions you were given, then it can't work anyway. The instructions ends with "... a working program takes test cases from the input file." If the program takes input from a file, then it is not reading from STDIN, and would ignore the data from the pipe anyway.

To make it work with a pipe, my_program has to be written to read from the STDIN, as in expecting you to type the test cases by hand at a prompt. Then you could rewrite the command line as

cat text_cases.txt | jave my_program

cat is a process that will read the text file and send its contents to STDOUT, then my_program would "read" the data from STDIN using the pipe instead of you typing it manually. Since I don't know how java connects with pipes, this is based on the presumption that it will behave in a standard way, since the instructor asked you to use that method.

IMHO it would be better, as in less resource usage, to use redirection than a pipe.

java my_program < test_cases.txt

That is, unless this is one step that will be included in a larger chain of processes later in the course where using a pipe will become necessary.

Share:
14,581

Related videos on Youtube

夢のの夢
Author by

夢のの夢

Updated on September 18, 2022

Comments

  • 夢のの夢
    夢のの夢 over 1 year

    My instructor says to use a pipe to apply a text file, which consists a list of test cases, to a working program that takes test case from the input file. Say i have

    test_cases.txt
    my_program //my java program after compliation
    

    and when I did this

    java my_program | test_cases.txt
    

    it gives

    [1]+ Stopped               java my_program | test_cases.txt
    

    Not sure how to use pipe...

    • Michael Homer
      Michael Homer over 7 years
      "apply a text file" is a meaningless phrase.