Become Our Fan on Social Sites!

Facebook Twitter

Google+ RSS YouTube

Thursday 3 October 2013

Read text file using C language

Following C program reads given text file and print its contents on the screen.


#include<stdio.h>  
 #include<stdlib.h>  
 void main()  
 {  
   char ch, file_name[50];  
   FILE *fp;  
   printf("Enter Filename : ");  
   gets(file_name);  
   /* "r" stands for reading mode */  
   fp = fopen(file_name, "r");  
   if(fp == NULL)  
   {  
     printf("File opening error");  
     exit(0);  
   }  
   printf("The contents of file is : \n");  
   while( ( ch = fgetc(fp) ) != EOF )  
     printf("%c", ch);  
   fclose(fp);  
 }  

0 comments :

Post a Comment