Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Endfile loops with data sets


1. Endfile loops with data sets
The endfile loop explicitly detects the end the input at the end of the file.

The EOF (End Of File) is the end of that input stream, such as a keyboard for the standard input, an input file (for the standard input), an actual file, or some other stream. The endfile loop is most similar to the trailer loop. (Likewise, the header loop is most similar to the counter loop)

There can only be one endfile loop for each file.
How is and endfile loop best done when each line of data has more than one number? Let us look at some examples with more than one data number per line.

2. More information
See the following for more information.

3. Method that does not always work
Many programming languages either raise a run-time error when trying to read past the end of file, or provide no run-time error but may or may not work as desired. C is such a language.

Here is the C code.


4. Examples of input and output
Here are some examples of input and output for the above program code.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input. For the above example input, here is the expected output.


5. Alternative method that works
When there is no data to be read, the programming language C will just read empty values, zeros for numbers, without an error.

Some programming languages will raise an error if an attempt is made to read beyond the end of the data.

Here is a way to handle this. Note only one value is input before determining whether the trailer value has been read. Here is the C code.


6. Examples of input and output
Here are some examples of input and output for the above program code.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input. For the above example input, here is the expected output.


7. Empty file
Note: In the above, note that when there is no input or input file, no input file is displayed.

8. Exercises
As an exercise, try changing the above examples to use parallel arrays of element variables and then an array of records with named field variables.

9. End of page

10. Multiple choice questions for this page

11. Acronyms and/or initialisms for this page