1

I just wondering if some one can confirm for me that I understand what exactly is going on here when a file gets piped into another program.

  1. Generate is producing a certain number of random numbers and then saves it in a text file.

  2. Then the numbers are being fed into find.c terminal one at a time the first time every time the find function needs a command line argument. (It doesn't seem to matter here if there are more text lines than command lines arguments. The program knows never feed text directly into the initial main command line.)

  3. When the last number is generated, the generate text file sends the End of File INT_MAX int to find.c which causes the function to BREAK.

Is there a place where these builtin functions are defined?

1 Answer 1

1
  1. Generate is producing a certain number of random numbers and then saves it in a text file.

  2. Then the numbers are being fed into find.c terminal one at a time the first time every time the find function needs a command line argument. (It doesn't seem to matter here if there are more text lines than command lines arguments. The program knows never feed text directly into the initial main command line.)

  3. When the last number is generated, the generate text file sends the End of File INT_MAX int to find.c which causes the function to BREAK.

No, generate doesn't output the pseudo-random numbers to a file. Rather, it outputs them to the standard output -- the console (or the terminal) in this case.

In case you wanna save the output to a text file, you may use redirection. The idea behind redirection is simple. You just redirect the standard output from the console, which is the default in this case, to a file. You can perform redirection using the greater than symbol (>) as follows

$ ./generate 10 > numbers.txt

in the previous example, generate would generate 10 pseudo-random numbers and saves them in a file named numbers.txt each in it's own separate line (just as it would print them to the console).

Similarly, you can redirect the standard input using the less than symbol (<). For example,

$ ./find 3 < numbers.txt

would read the numbers as it'd read them from the console (each number is followed by a newline character), but rather, it'd read them from the file named numbers.txt storing each number in a slot of haystack (i.e., the array) and continue the normal flow of sorting and searching for the number 3 (i.e., the needle).

This is known as redirection. Now, the question that might have come to your mind, how's that different from piping?

Well, piping doesn't involve outputting data to or inputting data from a file. When you pipe generate into find,

$ ./generate 10 | ./find 3

you're basically telling the computer "run this program named generate, redirect its standard output to be the standard input of the program named find". So, obviously, two redirection processes happened here -- redirecting the standard output of generate in addition to redirecting the standard input of find. In other words, piping is a chain of standard streams.

More on piping can be found here!

Hope that helps!


Update #0:

INT_MAX is just a macro for the constant integer value 2147483647 which is the maximum signed int value. This macro is defined in a file named limits.h which is usually under /usr/include/ and happens to be included to find through cs50.h.

It appears that GetInt() gets an int using GetString() which happens to be returning NULL in case of an error or the end-of-file (aka EOF) was inputted.

Per the definition of GetInt(), if GetString() returns NULL (e.g., in case of an error or the user inputted the EOF), the value INT_MAX is returned.

The EOF is the value the marks the end of a stream. This value can be entered from the keyboard using the key combination Ctrl + D under Unix-like systems (e.g., the appliance) or Ctrl + Z under Windows systems.

5
  • Thanks that's a clear explanation, and I was making the process seem more complicated than it was. Just one last question: The End of file integer, which I gather is INT_MAX, is automatically and always sent with the Standard output into standard input during this process? Sep 13, 2014 at 12:48
  • @WilliamKenney not exactly. Look at the update #0 section of my answer please!
    – kzidane
    Sep 13, 2014 at 13:04
  • thanks for your answer! Ok I see that I should have said that EOF input eventually leads to an INT_MAX return in the GetInt function. I think that the important point is that the find.c design with (if INT_MAX //BREAK) is the basic model for piping because the function will stop when there is no more output. Thanks again for your answer, I feel I am more on the right track now! Sep 13, 2014 at 15:34
  • @WilliamKenney usually it's tested whether the EOF was reached, but GetInt() hides that from us providing another value to indicate that there's no more input. This value is INT_MAX. Your question was a good question. Glad to know that you get it now!
    – kzidane
    Sep 13, 2014 at 17:28
  • It should also be noted that redirecting program output into another program as input (like ./find 127 < ./generate 1000 50) is not possible.
    – Cathryne
    Mar 17, 2016 at 16:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .