

fgetc()                   STDIO Function                  fgetc()




Read character from stream

#include <stdio.h>
iinntt ffggeettcc(_f_p) FFIILLEE *_f_p;

fgetc reads characters from  the input stream fp.  In general, it
behaves  the same  as the  macro getc: it  runs more  slowly than
getc, but yields a smaller object module when compiled.

***** Example *****

This example  counts the number  of lines and  ``sentences'' in a
file.


#include <stdio.h>



main()
{
        FILE *fp;
        int filename[20];
        int ch;
        int nlines = 0;
        int nsents = 0;



        printf("Enter file to test: ");
        gets(filename);



        if ((fp = fopen(filename,"r")) == NULL) {
                printf("Cannot open file %s.\n", filename);
                exit(1);
        }



        while ((ch = fgetc(fp)) != EOF) {
                if (ch == '\n')
                        ++nlines;



                else if (ch == '.' || ch == '!' || ch == '?') {
                        if ((ch = fgetc(fp)) != '.')
                                 ++nsents;






COHERENT Lexicon                                           Page 1




fgetc()                   STDIO Function                  fgetc()



                        else
                                while((ch=fgetc(fp)) == '.')
                                        ;
                        ungetc(ch, fp);
                }
        }



        printf("%d line(s), %d sentence(s).\n",
                nlines, nsents);
}


***** See Also *****

getc(), STDIO

***** Diagnostics *****

fgetc returns EOF at end of file or on error.




































COHERENT Lexicon                                           Page 2


