How it works...

The file whose name is supplied as a command-line argument is opened in read-only mode and is pointed to by the file pointer, fp. This recipe is focused on reading a file and changing its case, so if the file does not exist or does not have read permission, an error will be displayed and the program will terminate.

A while loop will be set to execute until feof (the end of file) is reached. Within the while loop, each line of the file will be read one by one and assigned to the string named buffer. The fgets() function will be used to read one line at a time from the file. A number of characters will be read from the file until the newline character, , is reached, to a maximum of 254.

The following steps will be performed on each of the lines assigned to the string buffer:

  1. The length of the buffer string will be computed and a for loop will be executed to access each of the characters in the string buffer.
  2. The string buffer will be checked to see whether there are any periods in it.
  3. If one is found, the character following it will be checked to see whether it is into lowercase. ASCII values will be used to then convert the lowercase characters into uppercase (refer to Chapter 2Managing Strings, for more information on the ASCII values that correspond to the letters of the alphabet). If the character following the period is in lowercase, a value of 32 will be deducted from the ASCII value of the lowercase character to convert it into uppercase. Remember, the ASCII value of uppercase characters is lower by a value of 32 than their corresponding lowercase characters.
  4. The updated string buffer with the character following the period converted into uppercase will be displayed on the screen.

When all the lines of the file are read and displayed, the file pointed to by the fp pointer will be closed.

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

D:CBook>gcc convertcase.c -o convertcase

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

Let's say that I have created a file called textfile.txt with the following content:

D:CBook>type textfile.txt
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 preceding command is executed in Windows' Command Prompt.

Let's run the executable file, convertcase.exe, and then pass the textfile.txt file to it, as shown in the following code:

D:CBook>./convertcase textfile.txt
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

You can see in the preceding output that the characters that were in lowercase after the period are now converted into uppercase.

Let's move on to the next recipe!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset