Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Opening File in C

Program to Open a Text file and Display the content
#include<stdio.h>
#include<stdlib.h>
 
main()
{
   char ch, file_name[25];
   FILE *fp;
 
   printf("Enter the name of file you wish to see ");
   gets(file_name);
 
   fp = fopen(file_name,"r"); // read mode
 
   if( fp == NULL )
   {
      perror("Error while opening the file.\n");
      exit(EXIT_FAILURE);
   }
 
   printf("The contents of %s file are :- \n\n", file_name);
 
   while( ( ch = fgetc(fp) ) != EOF )
      printf("%c",ch);
 
   fclose(fp);
   return 0;
}