In this video, we have an example of reading from a file with fgets,
which reads lines of a specified length from a file.
In this example, we will convert each line of text to an integer with the function atoi,
add each integer to the sum and print the result.
For the purposes of this example,
the array that we're going to read
our characters into is going to have a relatively small size.
We define line size here as a constant five,
make the array that big and also tell
fgets that we want to read into an array of size five.
We're going to do this for the purposes of example so we can
see what happens when fgets cannot read all of the characters on the line.
As before, we've omitted some error checking,
assuming that we would print an error message and exit under these two conditions.
We begin with our execution arrow just inside main where argc has
the value 2 and argv is a pointer to the array of command line arguments.
We assume that the name of the program is sum numbers and
that argv at one is the name of the input file,
nums.txt, which has the numbers in it.
We see if argc is not equal to 2.
It is 2, so we don't do any of that error code.
We're going to open this file for reading as we've seen before.
The file has numbers in it,
and the current position the file is at the start.
F is not null,
so we don't do anything special there.
We make a box for total which is zero,
then we create the five boxes for line,
each of which will hold a character.
Now, we're ready to call fgets.
It's going to read into line,
filling in the boxes with the information it reads from the file.
We tell it that there are at most five boxes that it can read into,
which really means four characters plus a null terminator character,
which it has to put at the end.
The last argument tells it to read from F. As we do this,
notice that the cursor in the file showing the current position is at the start,
and it's going to read across the line as we fill in the boxes with the text.
So we just read 123 and a new line,
as well as a null terminator into line,
which filled the five boxes.
And now the current position is in the file is at the start of the next line.
Remember that the function strchr takes a string and a character and
returns a pointer to the first occurrence of that character if it is found in the string.
If it is not found, it returns null.
So this is going to check if the line contains a new line,
letting us know if fgets was not able to read the entire line.
In this case, it returns a pointer to the fourth box,
which is not null,
so line contains a new line,
and everything is good.
We're now going to convert the text 1, 2,
3 to the integer 123,
and add it to total which is zero.
Zero plus 123 is 123,
so we change total to 123,
and we return to the top of the while.
Now, we're going to call fgets another time.
It's going to read this next line into the variable line.
Notice that it wrote one for new line,
then the null terminator here and left
the last box unchanged because it had already read the entire line.
We again do strchr of line and the new line character.
We find that it is not null,
then we add 14 to 123,
and we get 137.
We update total to 137 and we go around the loop again.
Notice that the number on the next line,
negative 56789 is too big to fit into the variable line.
It has more than the four characters we're able to read.
So fgets is going to read as much as it can and then stop.
It's going to read negative 567,
and then it's going to stop because we told it that
the array has at most five characters,
and it needs the last one for the null terminator.
The position in the file is between seven and eight.
If we were to read again,
we would read eight and nine whether we use fgets
or fgetc or any other function that reads from a file.
However, for this code,
it's not set up to be able to handle that,
so we have error checking for not reading the whole line.
If we were to make line size bigger,
we would make this all succeed.
Here, we do the check for the new line character,
strchr returns null, and will do the error handling.
We print, "Line is too long," and we exit failure.
Notice that we haven't closed the file,
since we'll learn how to do that shortly.