fgetc() prototype
int fgetc(FILE* stream);
The fgetc()
function takes a file stream as its argument and returns the next character from the given stream as a integer type.
It is defined in <cstdio> header file.
fgetc() Parameters
stream: The file stream to read the character.
fgetc() Return value
- On success, the fgetc() function returns the read character.
- On failure it returns EOF. If the failure is caused due to end of file, it sets the eof indicator. If the failure is caused by other errors, it sets the error indicator.
Example: How fgetc() function works
#include <cstdio>
int main()
{
int c;
FILE *fp;
fp = fopen("file.txt","r");
if (fp)
{
while(feof(fp) == 0)
{
c = fgetc(fp);
putchar(c);
}
}
else
perror("File opening failed");
fclose(fp);
return 0;
}
When you run the program, a possible output will be:
File handling example