Generate is producing a certain number of random numbers and then saves it in a text file.
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.)
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.