COHERENT manpages

This page displays the COHERENT manpage for fgetc() [Read character from stream].

List of available manpages
Index


fgetc() -- STDIO Function (libc)

Read character from stream
#include <stdio.h>
int fgetc(fp) FILE *fp;

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;

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

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

See Also

getc(),
libc
ANSI Standard, §7.9.7.1
POSIX Standard, §8.1

Diagnostics

fgetc() returns EOF at end of file or on error.