luc095.c (870B)
1 /* Write a program that can read a file and display its contents. The file name should be supplied as a command-line argument. 2 */ 3 /* Let Us C, Chap- 20 (More Issues In Input/Output), Qn No.: A(a) */ 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 *fp; 17 char ch; 18 19 /* Check if file name is provided */ 20 if (argc != 2) 21 { 22 printf("Usage: %s <filename>\n", argv[0]); 23 exit(1); 24 } 25 26 fp = fopen(argv[1], "r"); 27 if (fp == NULL) 28 { 29 printf("Error: Cannot open file '%s'\n", argv[1]); 30 exit(2); 31 } 32 33 /* Read and display contents */ 34 while ((ch = fgetc(fp)) != EOF) 35 { 36 printf("%c", ch); 37 } 38 39 fclose(fp); 40 return 0; 41 }