luc086.c (1275B)
1 /* Write a program to copy contents of one file to another. While doing so replace all lowercase characters to their equivalent uppercase characters. 2 */ 3 /* Let Us C, Chap- 19 (File Input/Output), Qn No.: B(b) */ 4 5 /* This file is auto-generated by a bot. */ 6 /* This code is not compiled; it is for reference only. */ 7 8 9 #include <stdio.h> 10 #include <stdlib.h> 11 #include <string.h> 12 #include <ctype.h> 13 14 void create_source_file(); 15 16 int main() 17 { 18 FILE *fs, *ft; 19 char ch; 20 21 create_source_file(); // Helper to make code runnable 22 23 fs = fopen("source.txt", "r"); 24 if (fs == NULL) 25 { 26 printf("Cannot open source file.\n"); 27 exit(1); 28 } 29 30 ft = fopen("target.txt", "w"); 31 if (ft == NULL) 32 { 33 printf("Cannot open target file.\n"); 34 fclose(fs); 35 exit(2); 36 } 37 38 while ((ch = fgetc(fs)) != EOF) 39 { 40 ch = toupper(ch); 41 fputc(ch, ft); 42 } 43 44 printf("File copied successfully with uppercase conversion.\n"); 45 printf("Check 'target.txt' for results.\n"); 46 47 fclose(fs); 48 fclose(ft); 49 50 return 0; 51 } 52 53 void create_source_file() 54 { 55 FILE *fp = fopen("source.txt", "w"); 56 if (fp) 57 { 58 fprintf(fp, "This is a sample text.\nIt contains Lowercase letters.\n"); 59 fclose(fp); 60 } 61 }