Read Contents of Text File C Program

By | December 11, 2015

C Program To Read Contents of Text File

Learn How To Read Contents of a Text File in C Programming Language. In order to read the content of any Text File, you need to provide the Mode of Operation in the fopen() function. It could be one of the following:

  • Read Mode
  • Write Mode
  • Append Mode

Apart from the above modes of editing text files, there can be numerous other combinations too. In this C Program, we have used Read Mode denoted by “r”.

Also Read: C Program To Count Number of Tabs, Spaces, Newlines, Characters in File

C Program To Read Contents of Text File


void main()
    FILE *fp;
    char ch;
    fp = fopen("/home/tusharsoni/Desktop/TestFile.txt","r");
    if(fp == NULL)
        printf("File Not Found\n");
            ch = fgetc(fp);
            if(ch == EOF)
            printf("%c", ch);


Read Contents of Text File in C

If you have any compilation errors or doubts in this C Program To Display the Contents of a Text File, let us know about it in the Comment Section below.

Let's Discuss