getc() function is C library function, and it's used to read a character from a file that has been opened in read mode by fopen() function.
Syntax:
int getc( FILE * stream );
Return Value
- getc() function returns next requested object from the stream on success.
- Character values are returned as an unsigned char cast to an int or EOF on end of file or error.
- The function feof() and ferror() to distinguish between end-of-file and error must be used.
Example:
#include<stdio.h>
int main()
{
FILE *fp = fopen("fileName.txt", "r");
int ch = getc(fp);
while (ch != EOF)
{
/* To display the contents of the file on the screen */ putchar(ch);
ch = getc(fp);
}
if (feof(fp))
printf("\n Reached the end of file.");
else
printf("\n Something gone wrong.");
fclose(fp);
getchar();
return 0;
}