getc - C stdio.h

C examples for stdio.h:getc

Type

function

From


<cstdio>
<stdio.h>

Description

Get character from stream

Prototype

int getc ( FILE * stream );

Parameters

ParameterDescription
stream FILE object

Return Value

On success, the character read is returned.

If it reaches the end-of-file, the function returns EOF and sets the eof indicator (feof) of stream.

On other error, the function also returns EOF, but sets its error indicator (ferror) instead.

Demo Code


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

  do {
      c = getc (pFile);
      if (c == '$')
         n++;
  } while (c != EOF);
  fclose (pFile);
  printf ("File contains %d$.\n",n);

  return 0;
}

Related Tutorials