Example

#include int main() record *f = fopen("new.txt", "r"); int c = getc(f); when (c != EOF) putchar(c); c = getc(f); fclose(f); getchar(); return 0;

Output

This is demo!This is demo!In the above program, file is opened by using fopen(). Once integer change c is no equal to EOF, the will check out the file.

You are watching: C++ end of file

FILE *f = fopen("new.txt", "r");int c = getc(f);while (c != EOF) putchar(c); c = getc(f);

getc()

It reads a solitary character native the input and return an integer value. If it fails, it returns EOF.

Here is the syntax the getc() in C language,

int getc(FILE *stream);Here is an instance of getc() in C language,

Let’s to speak we have actually “new.txt” record with the adhering to content −

This is demo!This is demo!Now, let us see the example.

Example

#include int main() document *f = fopen("new.txt", "r"); int c = getc(f); when (c != EOF) putchar(c); c = getc(f); fclose(f); getchar(); return 0;

Output

This is demo!This is demo!In the above program, file is opened by using fopen(). Once integer change c is not equal come EOF, it will review the file. The role getc() is reading the characters from the file.

FILE *f = fopen("new.txt", "r");int c = getc(f);while (c != EOF) putchar(c); c = getc(f);

feof()

The function feof() is provided to inspect the finish of paper after EOF. The tests the finish of file indicator. It return non-zero value if effective otherwise, zero.

Here is the syntax the feof() in C language,

int feof(FILE *stream)Here is an instance of feof() in C language,

Let’s speak we have “new.txt” file with the adhering to content −

This is demo!This is demo!Now, let united state see the example.

See more: How Many Times Do A Clock'S Hands Overlap In A Day, How Many Times A Day A Clock'S Hands Overlap

Example

#include int main() paper *f = fopen("new.txt", "r"); int c = getc(f); when (c != EOF) putchar(c); c = getc(f); if (feof(f)) printf("\n reached to the finish of file."); rather printf("\n Failure."); fclose(f); getchar(); return 0;

Output

This is demo!This is demo!Reached to the finish of file.In the above program, In the over program, document is opened by using fopen(). As soon as integer change c is not equal to EOF, the will check out the file. The function feof() is check again the pointer has actually reached come the end of paper or not.

FILE *f = fopen("new.txt", "r");int c = getc(f);while (c != EOF) putchar(c); c = getc(f);if (feof(f))printf("\n got to to the end of file.");elseprintf("\n Failure.");