cplusplus.com > reference > cstdio > fgetc
fgetc
<stdio.h>
  cplusplus.com  
int  fgetc (FILE * stream);

Get the next character from a stream.
Returns the next character of the stream and increases the file pointer to point to the following one.

Parameters.

stream
pointer to an open file.

Return Value.
  The character read is returned as an int value.
  If the End Of File has been reached or there has been an error reading, the function returns EOF. You can use ferror() or feof() to determine whether an error happened or the End-Of-File was reached.
 

Portability.
  Defined in ANSI-C.

Example.

/* fgetc example: money counter */
#include <stdio.h>
int main ()
{
  FILE * pFile;
  char c;
  int n = 0;
  pFile=fopen ("myfile.txt","r");
  if (pFile==NULL) perror ("Error opening file");
  else
  {
    do {
      c = fgetc (pFile);
      if (c == '$') n++;
    } while (c != EOF);
    fclose (pFile);
    printf ("File contains %d$.\n",n);
  }
  return 0;
}
  This program reads existing file myfile.txt character by character and uses the n variable to count how many dollar characters ($) does the file contain.

See also.
  fputc, fread, fwrite


© The C++ Resources Network, 2000