C Program to Read a File and Print its content

In this post, we are going to see how to write a simple C program to read a text file and print its content. We are going to use C pointers to read the file. So without wasting time let’s see the program.

Make sure you have the text file (myfile.txt) in the same place where you are going to execute the below C program:

The above code is self-explanatory. I have updated the comments inline. Just read them to understand it. You can set your file name in this variable called “fileName“.

An example output would be,

Scenario 1 – (if the file does not exist):

Scenario 2 – (If the file exist):

I hope this program clears your doubts on how to read a text file using C programming and display its content.



Related Posts

Leave a Reply