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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include<stdio.h> #include<stdlib.h> void main() { FILE *fp; char ch; fp = fopen("/home/tusharsoni/Desktop/TestFile.txt","r"); if(fp == NULL) { printf("File Not Found\n"); exit(1); } else { while(1) { ch = fgetc(fp); if(ch == EOF) { break; } printf("%c", ch); } } printf("\n"); fclose(fp); } |
Output

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.