luc096.c (1331B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 08 Feb 2026 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* Write a program that can copy the contents of one file to another. The source and target filenames should be supplied as command-line arguments. 9 */ 10 /* Let Us C, Chap- 20 (More Issues In Input/Output), Qn No.: A(b) */ 11 12 /* This file is auto-generated by a bot. */ 13 /* This code is not compiled; it is for reference only. */ 14 15 16 #include <stdio.h> 17 #include <stdlib.h> 18 #include <string.h> 19 #include <ctype.h> 20 21 int main(int argc, char *argv[]) 22 { 23 FILE *fs, *ft; 24 char ch; 25 26 /* Check for correct number of arguments */ 27 if (argc != 3) 28 { 29 printf("Usage: %s <source_file> <target_file>\n", argv[0]); 30 exit(1); 31 } 32 33 fs = fopen(argv[1], "r"); 34 if (fs == NULL) 35 { 36 printf("Error: Cannot open source file '%s'\n", argv[1]); 37 exit(2); 38 } 39 40 ft = fopen(argv[2], "w"); 41 if (ft == NULL) 42 { 43 printf("Error: Cannot create target file '%s'\n", argv[2]); 44 fclose(fs); 45 exit(3); 46 } 47 48 /* Copy contents */ 49 while ((ch = fgetc(fs)) != EOF) 50 { 51 fputc(ch, ft); 52 } 53 54 printf("File copied successfully.\n"); 55 56 fclose(fs); 57 fclose(ft); 58 return 0; 59 }