3

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...

  • 3
    "apply a text file" is a meaningless phrase. – Michael Homer Feb 6 '17 at 4:24
6

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.