How to use feof() function to checks whether the end of file reached in C

2 Answers

0 votes
#include <stdio.h>

int main(void)
{
	FILE *fp  = fopen("d:\\data.txt", "r");
	
	if (fp == NULL) 
		perror("Error open file\n");
	else
	{
		char ch;
		while ( (ch = fgetc(fp)) != EOF) 
			printf("%c", ch);
    }
    if (feof(fp)) 
		puts("\nEnd of File\n");
    else 
		puts ("\nNot End of File\n");
		
	fclose(fp);
    
	return 0;
}
 
 
/*
run:
 
code for fun
End of File

*/

 



answered May 2, 2016 by avibootz
0 votes
#include <stdio.h>

int main(void)
{
	FILE *fp  = fopen("d:\\data.txt", "r");
	
	if (fp == NULL) 
		perror("Error open file\n");
	else
	{
		char ch;
		while(1)
		{
			ch = fgetc(fp);
			if (feof(fp))
				break;
			printf("%c", ch);
		}
    }
    if (feof(fp)) 
		puts("\nEnd of File\n");
    else 
		puts ("\nNot End of File\n");
		
	fclose(fp);
    
	return 0;
}
 
 
/*
run:
 
code for fun
End of File

*/

 



answered May 2, 2016 by avibootz

Related questions

...