COHERENT manpages

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

List of available manpages
Index


getc() -- STDIO Function (libc)

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

getc() is  a function that reads  a character from the  file stream fp, and
returns an int.

Example

The following  example creates a  simple copy utility.  It  opens the first
file named on the command line and copies its contents into the second file
named on the command line.

#include <stdio.h>

void fatal(string)
char *string;
{
    printf("%s\n", string);
    exit (1);
}

main(argc, argv)
int argc; char *argv[];
{
    int foo;
    FILE *source, *dest;

    if (--argc != 2)
        fatal("Usage: copy [source][destination]");

    if ((source = fopen(argv[1], "r")) == NULL)
        fatal("Cannot open source file");
    if ((dest = fopen(argv[2], "w")) == NULL)
        fatal("Cannot open destination file");

    while ((foo = getc(source)) != EOF)
        putc(foo, dest);
}

See Also

fgetc(),
getchar(),
libc,
putc()
ANSI Standard, §7.9.7.5
POSIX Standard, §8.1

Diagnostics

getc() returns EOF at end of file or on read fatal.

Notes

Because getc()  is a macro,  arguments with side effects  probably will not
work as  expected.  Also,  because getc()  is a complex  macro, its  use in
expressions of  too great a  complexity may cause  unforeseen difficulties.
Use of the function fgetc() may avoid this.