fgetc - C stdio.h

C examples for stdio.h:fgetc

Type

function

From


<cstdio>
<stdio.h>

Prototype

int fgetc ( FILE * stream );

Description

Read a character from the specified stream.

Parameters

ParameterDescription
stream Pointer to a FILE object that identifies an input stream.

Return Value

On success, the character read is returned.

If the position indicator was at the end-of-file, the function returns EOF.

Example

The following code reads an existing file character by character.

It counts how many dollar characters ($) the file contains.

Demo Code


#include <stdio.h>
int main ()//from  w ww .j  a v a2s .  c  o m
{
  FILE * pFile;
  int c;
  int n = 0;
  pFile=fopen ("main.cpp","r");
  if (pFile==NULL) {
     perror ("Error opening file");
     return -1;
  }

  do {
      c = fgetc (pFile);
      if (c == '$') n++;
  } while (c != EOF);
  fclose (pFile);
  printf ("The file contains %d dollar sign characters ($).\n",n);

  return 0;
}

Related Tutorials