About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Tuesday, September 3, 2013

C program to Convert the file contents in Upper-case & Write Contents in a Output file

Write a program to read a text file and convert the file contents in capital (Upper-case) and write the contents in a output file. Program to copy the contents of one file into another by changing case.
#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.

No comments: