C examples for stdio.h:fgetc
function
<cstdio> <stdio.h>
int fgetc ( FILE * stream );
Read a character from the specified stream.
Parameter | Description |
---|---|
stream | Pointer to a FILE object that identifies an input stream. |
On success, the character read is returned.
If the position indicator was at the end-of-file, the function returns EOF.
The following code reads an existing file character by character.
It counts how many dollar characters ($) the file contains.
#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; }