luc096.c (1140B)
1 /* 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. 2 */ 3 /* Let Us C, Chap- 20 (More Issues In Input/Output), Qn No.: A(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 int main(int argc, char *argv[]) 15 { 16 FILE *fs, *ft; 17 char ch; 18 19 /* Check for correct number of arguments */ 20 if (argc != 3) 21 { 22 printf("Usage: %s <source_file> <target_file>\n", argv[0]); 23 exit(1); 24 } 25 26 fs = fopen(argv[1], "r"); 27 if (fs == NULL) 28 { 29 printf("Error: Cannot open source file '%s'\n", argv[1]); 30 exit(2); 31 } 32 33 ft = fopen(argv[2], "w"); 34 if (ft == NULL) 35 { 36 printf("Error: Cannot create target file '%s'\n", argv[2]); 37 fclose(fs); 38 exit(3); 39 } 40 41 /* Copy contents */ 42 while ((ch = fgetc(fs)) != EOF) 43 { 44 fputc(ch, ft); 45 } 46 47 printf("File copied successfully.\n"); 48 49 fclose(fs); 50 fclose(ft); 51 return 0; 52 }