C Program to Read File

« Previous Program Next Program »

Read File in C

To read a file in C programming, you have to first open that file using the function fopen() and then start reading the file's content character by character.

C Programming Code to Read File

Following C program opens a file named filename.txt to read the content present inside this file, if there is an error in opening a file then program puts a message on the screen for the error, and if the file will be read then it will display the file, but this program limits to only one line of the file which is to be read. To know more, go to 2nd next program which will clear your doubt, that program will read a file and then display the contents of it. For now, go through the following program which will read a file and display its content on the screen:

/* C Program - Read a File */
void main()
	char c[1000];
	FILE *fp;
	if((fp=fopen("filename.txt", "r"))==NULL)
		printf("Error in opening file..!!");
	fscanf(fp, "%s", c);
	printf("Data in file = %s",c);

Same Program in Other Programming Language

You may also like same program in other programming languages:

« Previous Program Next Program »


Quick Links
Signup - Login - Give Online Test