C program to read contents of a file and print all characters in uppercase

C program to read contents of a file and print all characters in uppercase :

In this C programming tutorial, we will learn how to read all characters of a file and print all the characters in uppercase. For example, if the file contains hello world as text, it will print HELLO WORLD on the console. To solve this program, first we will read the contents of the file, then convert each character to uppercase and finally print out the result. Let’s take a look at the program :

C program :

    #include<stdio.h>
    #include<ctype.h>
    
    int main(){
        //1
        FILE *file;
        char ch;
        char fileName[100] = "temp.txt";
    
        //2
        file = fopen(fileName,"r");
    
        //3
        if(file == NULL){
            printf("File not found.");
        }else{
            //4
            ch = fgetc(file);
    
            //5
            while(ch != EOF){
                printf("%c",toupper(ch));
                ch = fgetc(file);
            }
            fclose(file);
        }
    }

Explanation :

The commented numbers in the above program denote the step number below :

  1. Create one FILE pointer file,one character variable ch and one character array fileName to hold the file path.
  2. Open the file and store it in file variable.
  3. Check if the file is NULL or not. If NULL , means the file is not available. So, print one message that the file is not found.
  4. Else, read the first character of the file and store it in the variable ch.
  5. Run one while loop . This loop will run till the value of ch becomes end of the file. Inside the loop, read the next character of the file , convert it to uppercase using toupper() function and print it out. After all characters are read, close the file using fclose function.

It will print out the contents of the text file.

Where is the color and why codevscolor ?

Long story short, I love paintings and I paint on weekends. We(me and my wife) have one Youtube channel. Below is a video that I did recently. If you love this please do subscribe to support us 😊