getc() STDIO Macro getc() Read character from file stream #include <stdio.h> int getc(fp) FILE *fp; getc is a macro 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); } COHERENT Lexicon Page 1
getc() STDIO Macro getc() ***** See Also ***** fgetc(), getchar(), putc(), STDIO ***** 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. COHERENT Lexicon Page 2