Reads any text file and displays the contents on the screen. - C File

C examples for File:Text File

Description

Reads any text file and displays the contents on the screen.

Demo Code

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
   FILE *fp;/*from  ww  w. j  ava  2s  .com*/
   char ch;

   if (argc != 2) {
      printf("You forgot to enter the filename.\n");
      exit(1);
   }

   if ((fp = fopen(argv[1], "r")) == NULL) {
      printf("Cannot open file.\n");
      exit(1);
   }
   ch = getc(fp);   /* read one character */

   while (ch != EOF) {
      putchar(ch);  /* print on screen */

      ch = getc(fp);
   }

   fclose(fp);

   return 0;

}

Result


Related Tutorials