fgetc() - I/O Function

How is "fgetc()" used in C++?

Explanation

fgetc() is an I/O function that returns the next character of the input stream, then increments the file position indicator. The value is read as an unsigned char after which it is converted to an integer. If the EOF is reached or an error occurs the function returns EOF and the eof indicator is set. This function may mark the st_atime field of the file associated with stream for update.

Syntax:


int fgetc ( FILE * stream );

Example :



#include <stdio.h>
int main()
{
int c;
int count;
FILE *stri = fopen("getceg.txt", "r");
while (c != EOF)
{
c = fgetc (stri);
if (c == 'A') count++;
}
fclose (stri);
printf ("The file contains A %d times.\n",count);
return 0;
}

Result :

The file contains A 3 times

In the above example the "fgetc()" is used to check how many times the character 'A' is present in the
file "getceg.txt".

C++ Tutorial


Ask Questions

Ask Question