223k views
0 votes
What function can be used to detect when the end of an input file has been reached?

User HbnKing
by
5.1k points

1 Answer

3 votes

Answer:

getc() or feof() in c/c++.

Step-by-step explanation:

getc() returns EOF(End of File) when the end of the file reached is reached but it is not efficient because it also return EOF when it fails.

feof() returns non-zero value when the EOF is reached otherwise it return 0.So feof() is an efficient method to read a file.

For example:-

#include <stdio.h>

int main()

{

FILE *f = fopen("sample.txt", "r");

int c = getc(f);

while (c!= EOF)

{

putchar(ch);

ch = getc(f);

}

if (feof(f))

printf("\\ File has ended.");

else

printf("\\ Reading not happened.");

fclose(f);

getchar();

return 0;

}

User Chris Hawes
by
5.1k points