getc -- Read a Character from a File

SYNOPSIS

 #include <stdio.h>

 int getc(FILE *f);
 

DESCRIPTION

getc reads a single character from the stream associated with the FILE object addressed by f.

RETURN VALUE

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.

IMPLEMENTATION

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.

EXAMPLE

This example copies one file to another, translating all uppercase characters to lowercase characters, and all lowercase characters to uppercase characters.
  #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);
  }

 

RELATED FUNCTIONS

fgetc, getchar

SEE ALSO


Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.