fgetc()

QNX SDP8.0C Library ReferenceAPIDeveloper

Read a character from a stream

Synopsis:

#include <stdio.h>

int fgetc( FILE* fp );

Arguments:

fp
The stream from which you want to read a character.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The fgetc() function reads the next character from the stream specified by fp.

Returns:

  • If successful, fgetc() returns the next character from fp, cast as (int)(unsigned char).
  • If the end-of-file indicator for the stream is set or the end of the file has been reached, fgetc() sets the end-of-file indicator and returns EOF.
  • If an error occurred, fgetc() sets the error indicator for the stream, sets errno, and returns EOF.
Note:
Use feof() or ferror() to distinguish an end-of-file condition from an error.

Errors:

EAGAIN
The O_NONBLOCK flag is set for the file descriptor underlying fp, and the process would be delayed in the write operation.
EBADF
The file descriptor underlying fp isn't a valid file descriptor that's open for reading.
EINTR
The read operation was terminated due to the receipt of a signal, and no data was transferred.
EIO
One of the following:
  • A physical I/O error occurred.
  • The process is in a background process group attempting to read from its controlling terminal, and either the process is ignoring or blocking the SIGTTIN signal or the process group is orphaned.
  • (QNX OS extension) The filesystem resides on a removable media device, and the media has been forcibly removed.
ENOMEM
Insufficient space is available.
ENXIO
A request was made of a nonexistent device, or the request was outside the capabilities of the device.
EOVERFLOW
The file is a regular file, and an attempt was made to read at or beyond the offset maximum associated with the corresponding stream.

Examples:

#include <stdio.h>
#include <stdlib.h>

int main( void ) 
{
    FILE *fp;
    int c;

    fp = fopen( "file", "r" );
    if( fp != NULL ) {
        while( (c = fgetc( fp )) != EOF ) {
            fputc( c, stdout );
        }
        fclose( fp );
        
        return EXIT_SUCCESS;
    }
    
    return EXIT_FAILURE;
}

Classification:

ANSI, POSIX 1003.1

Safety:
Cancellation pointYes
Signal handlerNo
ThreadYes
Page updated: