HP C/iX Library Reference Manual (30026-90004)
194 Chapter5
HP C/iX Library Function Descriptions
getc
putc('\n', stdout);
exit(0);
}
This program accepts a single argument that is assumed to be the name of a file whose
contents are to be printed on the terminal. The specified file is opened for reading, and the
resulting file pointer fp is used in getc() to read a character from the file. Each character
read is written on stdout using putc(). (Note that stdout, stdin, and stderr are legal
file pointers.) The reading and writing loop terminates when the constant EOF is returned
from getc(), indicating that the end of the file has been reached. This constant is defined
in <stdio.h>.
You can use the flexibility of putc() to send data somewhere other than to the user's
terminal. For example, the file copy program from the previous example can be rewritten
using getc() and putc().
#include <stdio.h>
main(argc, argv)
int argc;
char *argv[ ];
{
int c;
FILE *from, *to;
if(argc != 3) {
printf("Usage: cp fromfile tofile\n");
exit(1);
}
from = fopen(argv[1], "r");
if(from == NULL) {
printf("Can't open %s.\n", argv[1]);
exit(1);
}
to = fopen(argv[2], "w");
if(to == NULL) {
printf("Can't create %s.\n", argv[2]);
exit(1);
}
while((c = getc(from)) != EOF)
putc(c, to);
exit(0);
}
See Also
fclose(), ferror(), fopen(), fread(), fgetc(), gets(), putc(), fputc(), scanf(),
ANSI C 4.9.7.5, POSIX.1 8.1