In C file handling, we can convert the contents of a file into capital letters while reading it. In this program, the source file is opened in read mode and another file is opened in write mode. Each character is checked, and if it is a small letter, it is converted into a capital letter using ASCII values.
How It Works?
"rt" mode and destination file in "wt" mode.fgetc().'a' and 'z'.fputc().fclose().#include <stdio.h> #include <conio.h> main() { FILE *fp, *fp2; char ch; clrscr(); fp = fopen("file2.c", "rt"); fp2 = fopen("filenew.c", "wt"); if(fp == NULL || fp2 == NULL) { printf("Error opening file(s)"); return; } while((ch = fgetc(fp)) != EOF) { if(ch >= 'a' && ch <= 'z') { ch = ch - 32; } fputc(ch, fp2); printf("%c", ch); } fclose(fp); fclose(fp2); getch(); }
Content of file2.c:
Output After Conversion:
data.txt in read mode ("rt") and capital.txt in write mode ("wt"). Check each character between 'a' and 'z' and subtract 32 to convert it into a capital letter.
#include <stdio.h> #include <conio.h> main() { FILE *fp, *fp2; char ch; clrscr(); fp = fopen("data.txt", "rt"); fp2 = fopen("capital.txt", "wt"); while((ch = fgetc(fp)) != EOF) { if(ch >= 'a' && ch <= 'z') { ch = ch - 32; } fputc(ch, fp2); printf("%c", ch); } fclose(fp); fclose(fp2); getch(); }
data.txt File Content:
capital.txt File Content:
data.txt one by one. If a character is a small letter, it is converted into a capital letter using ASCII value conversion. The converted characters are then stored in capital.txt using fputc().