C Program to Convert the File Contents in Upper-case & Write Contents in a Output File

C Program to Convert the File Contents in Upper-case & Write Contents in a Output File.



The C library function int toupper(int c) converts lowercase letter to uppercase.

The declaration for toupper( ) function is as follows:
                             
int toupper(int c );

c - This is the letter to be converted to uppercase.

This function returns uppercase equivalent to c, if such value exists, else c remains unchanged. The value is returned as an int value that can be implicitly casted to char.

The following example shows the usage of toupper( ) function.

--------------------------------------------------------------------------------------------------------------------------

#include<stdio.h>
int main( )
{
     int i=0;
     char c;
     char str[ ] = "Upper-Case";
     while(str[i])
     {
          putchar(toupper(str[i]));
          i++;
}
return (0);
}


--------------------------------------------------------------------------------------------------------------------------

#include<stdio.h>
#include<process.h>
void main( )
{
   FILE *fp1,*fp2;
   char a;
   clrscr( );
   fp1 = fopen("program1.txt","r");
   if(fp1 == NULL){
   puts("cannot open this file");
   exit(1);
   }
   fp2 = fopen("program2.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("program1.txt","r");
fp2 = fopen("program2.txt","w");

Read file character by character

toupper( ) function will convert lower case letter to uppercase.
do{
a = fgetc(fp1);
a = toupper(a);
fputc(a,fp2);
}while(a!=EOF);

We are writing character back to the file.

Whenever you find End of File character then terminate the process of reading the file and writing the file.



EmoticonEmoticon