Popular Tutorials
Start Learning C++Certification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
C++ fgetc()
The fgetc() function in C++ reads the next character from the given input stream.
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
Did you find this article helpful?