C Program to Open and Read Contents of File Using fopen Library

Here, You are going to execute the C Program to Open and Read Contents of File Using fopen Library using this article successfully. Let’s start the article to execute it successfully.

C Program to Open and Read Contents of File Using fopen Library

C Program to Open and Read Contents of File Using fopen Library

#include <stdio.h>
#include <stdlib.h>
int main()
{
   char ch, file_name[25];
   FILE *fp;

   printf("Enter name of a file you wish to seen");
   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", file_name);

   while((ch = fgetc(fp)) != EOF)
      printf("%c", ch);

   fclose(fp);
   return 0;
}

Conclusion

I hope this article about C Program to Open and Read Contents of File Using fopen Library will be useful to you. If you have any issues about this topic then ask us via the comment section. We are always ready to help you. Share this article with your friends to help them.

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment