assignment-p-14_v2.c (673B)
1 /* Write a C program that opens its own source code file, reads its contents, and then prints 2 the contents to the console. */ 3 4 #include <stdio.h> 5 6 #define FILENAME "assignment-p-14_v2.c" 7 8 int main() 9 { 10 FILE *code = NULL; 11 char temp; 12 13 code = fopen(FILENAME, "r"); 14 if (code == NULL) 15 { 16 printf("\nCould not open the source file: %s", FILENAME); 17 return 1; 18 } 19 20 printf("\nReading file: %s", FILENAME); 21 printf("\n========== %s ==========\n\n", FILENAME); 22 23 while ((temp = fgetc(code)) != EOF) 24 { 25 printf("%c", temp); 26 } 27 28 printf("\n\n========== End of %s ==========\n", FILENAME); 29 30 fclose(code); 31 32 return 0; 33 }