C program to display the Content in a File



By executing this program screen will display text in a file. you need to enter file name with extension and file should be in directory folder.


Program:
  
//Program To show content in file. by SlashMyCode.com  

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

int main()
{
   char ch, file_name[25];
   FILE *fp;

   printf("Enter the name of file you wish to see\n");
   gets(file_name);

   fp = fopen(file_name,"r");
        if( fp == NULL )
           {
              perror("Error while opening the file.\n");
              exit(EXIT_FAILURE);
           }

   printf("The contents of %s file are :\n", file_name);
 
  while( ( ch = fgetc(fp) ) != EOF )
      printf("%c",ch);


//Program To show content in file. by SlashMyCode.com  


   fclose(fp);
   return 0;
}




 
Output:

This is text document in directory folder, with name 'SlashMyCode.txt'.
 

 
Just enter the file name with its extension, and program will display text content in it.
 

No comments:

Post a Comment

Feel free to ask us any question regarding this post