#include <stdio.h> int getc(FILE *f);
getc
reads a single character from the stream associated with the
FILE
object addressed by f
.
getc
returns the next input character or EOF
if no character is
read. A return value of EOF
indicates that the end of file has been
reached or that an error has occurred. The ferror
function can be
called to distinguish these cases.
getc
is implemented as a built-in function. A subroutine call is
executed only if no characters remain in the current input buffer.
The code generated for getc
normally includes tests for a 0 FILE
pointer and for a stream that failed to open. If you define the symbol
_FASTIO
using #define
or the define
compiler option before
including <stdio.h>
, no code is generated for these checks. This enables you
to improve the performance of debugged programs that use getc
.
#include <ctype.h> #include <string.h> #include <stdlib.h> #include <stdio.h> #define _FASTIO /* Improve getc/putc performance. */ main() { int ch; FILE *in, *out; char filename[60]; puts("Enter the name of your input file:"); memcpy(filename, "cms:", 4); gets(filename+4); in = fopen(filename, "r"); if (!in){ puts("Failed to open input file."); exit(EXIT_FAILURE); } puts("Enter the name of your output file:"); memcpy(filename, "cms:", 4); gets(filename+4); out = fopen(filename, "w"); if (!out){ puts("Failed to open output file."); exit(EXIT_FAILURE); } for(;;){ ch = getc(in); if (ch == EOF) break; if (islower(ch)) putc(toupper(ch), out); else putc(tolower(ch), out); if (ferror(out)) break; } if (ferror(in) || ferror(out)) /* Check for error. */ exit(EXIT_FAILURE); exit(EXIT_SUCCESS); }
fgetc
, getchar
Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.