Skip to main content
Logo image

Section 15.2 Reading from a File

This program demonstrates how to open a file, read numbers from the file and then close the file.
How do we know how many numbers are stored in the file? In this particular example, we are told that there are 10 numbers stored in the file. Later, we will learn how to figure this information out on our own. Here is what the file looks like:
9  56  3  8  -11  0  -45  55  2  78

admin.....open in new window

Subsection 15.2.1 Your Turn: Calculating the Average of the Numbers Read from the File

Now, suppose we wanted to find the average of the numbers read from the same file as above.