How it works...

We will open the chosen sequential file in read-only mode. If the file opens successfully, it will be pointed at by the file pointer, fp. To count the number of vowels in the file, we will initialize a counter from 0.

We will set a while loop to execute until the file pointer, fp, reaches the end of the file. Within the while loop, each line in the file will be read using the fgets function. The fgets function will read the BUFFSIZE number of characters from the file. The value of the BUFFSIZE variable is 255, so fgets will read either 254 characters from the file or will read characters until the newline character, \n, is reached, whichever comes first.

The line read from the file is assigned to the buffer string. To display the file contents along with the count of the vowels, the content in the buffer string is displayed on the screen. The length of the buffer string will be computed and a for loop will be set to execute equaling the length of the string.

Each of the characters in the buffer string will be checked in the for loop. If any lowercase or uppercase vowels appear in the line, the value of the counter variable will be incremented by 1. When the while loop ends, the counter variable will have the total count of the vowels present in the file. Finally, the value in the counter variable will be displayed on the screen.

Let's use GCC to compile the countvowels.c program as follows:

D:\CBook>gcc countvowels.c -o countvowels

If you get no errors or warnings, then this means that the countvowels.c program has been compiled into an executable file called countvowels.exe.

Let's assume that we have a text file called textfile.txt with some content. We will run the executable file, countvowels.exe, and supply the textfile.txt file to it to count the number of vowels in it, as shown in the following code:

D:\CBook>./countvowels textfile.txt
The file content is :
I am trying to create a sequential file. it is through C programming. It is very hot today. I have a cat. do you like animals? It might rain. Thank you. bye
The number of vowels are 49

You can see from the output of the program that the program not only displays the count of the vowels, but also the complete content of the file.

Now, let's move on to the next recipe!