Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

fgetc - Read a character from a file

&pagelevel(4)&pagelevel

Definition  

#include <stdio.h>

int fgetc(FILE *fp);

fgetc reads a character from the file indicated by file pointer fp from the current read/write
position.

Return val.

integer

If successful, the character as a positive integer value.


EOF

for end of file or error.

Notes

fgetc behaves like getc (as a function). If you use a comparison such as

while((c = fgetc(fp)) != EOF)

in your program, the variable c must always be declared as an integer. If you define c as a char, the EOF condition is never satisfied for the following reason: -1 is converted to char0xFF’ (i.e. +255); EOF, however, is defined as -1.

If fgetc is reading from the standard input stdin, and EOF is the end criterion for reading, you can satisfy the EOF condition by means of the following actions at the terminal:
pressing the K2 key and entering the system commands EOF and RESUME-PROGRAM.

The following applies in the case of text files with SAM access mode and variable record length for which a maximum record length is also specified: When the specification split=no was entered for fopen, records of maximum length are not concatenated with the subsequent record when they are read. By default or with the specification split=yes, when a record with maximum record length is read, it is assumed that the following record is the continuation of this record and the records are concatenated.

Example

The following program successively reads one character at a time from a maximum of 10 files passed in the call and outputs the character on the standard output.

#include <stdio.h>
FILE *fp[10], **app;
int main(int argc, char *argv[])
{
  int c, i;
  for (i = 1; i < argc && i <= 10; i++)
       fp[i-1] = fopen(argv[i], "r");
  app = fp;
  while(*app != NULL)
       {
         c = fgetc(*app++);
         putchar(c);
       }
  putchar('\n');
  return 0;
}

See also

getc, getchar, ungetc, fopen, fopen64