C Program To Convert The File Contents In Upper-Case & Write Contents In A Output File

How To Write a C Program To Convert The File Contents In Upper-Case & Write Contents In A Output File in C Programming Language ?


Write a program to read a text file and convert the file contents in capital (Uppercase) and write the contents in a output file. Program to copy the contents of one file into another by changing case.

Solution For C Program To Convert The File Contents In Upper-Case & Write Contents In A Output File:
#include<stdio.h>
#include<process.h>

void main()
{
FILE *fp1,*fp2;
char a;
clrscr();

fp1=fopen("test.txt","r");
if(fp1==NULL)
     {
     puts("cannot open this file");
     exit(1);
     }

fp2=fopen("test1.txt","w");
if(fp2==NULL)
     {
     puts("Not able to open this file");
     fclose(fp1);
     exit(1);
     }

  do
    {
    a=fgetc(fp1);
    a=toupper(a);
    fputc(a,fp2);
    }while(a!=EOF);

fcloseall();
getch();
}

Explanation :

Open one file in the read mode another file in the write mode.
fp1=fopen("test.txt","r");
fp2=fopen("test1.txt","w");
Now read file character by character. toupper() function will convert lower case letter to upper case.
do   {
     a=fgetc(fp1);
     a=toupper(a);
     fputc(a,fp2);
}while(a!=EOF);
After converting into upper case, we are writing character back to the file. Whenever we find End of file character then we terminate the process of reading the file and writing the file.
Tags: C Program To Convert The File Contents In Upper-Case & Write Contents In A Output File, convert lowercase to uppercase in c program, c program to count no of lines, blank lines, comments in a given program, c program to convert lowercase to uppercase and vice versa, convert lowercase to uppercase in c without using function, c program to check uppercase or lowercase, toupper in c, tolower in c, file handling in c.


Learn More :