fgetc

 

Description

reads a single character from the current position of a file associated with stream.

Syntax

int fgetc( FILE * stream )

Parameters

stream
Pointer to FILE

Return

fgetc return the character read as an int or return EOF to indicate an error or end of file.

Use feof or ferror to distinguish between an error and an end-of-file condition. If a read error occurs, the error indicator for the stream is set.

Examples

EX1

//The following example uses getc to read the first 80 input characters(or until the end of input) 
//and place them into a string named buffer.

void test_fgetc()
{
    FILE *stream;
    char buffer[81];
    int  i, ch;
    
    // Open the current C file to read line from: 
    stream = fopen( __FILE__, "r" );
    if(stream == NULL )
        printf("The current C file cannot be opened");
    
    // Read in first 80 characters and place them in "buffer": 
    ch = fgetc( stream );
    for( i=0; (i < 80 ) && ( feof( stream ) == 0 ); i++ )
    {
        buffer[i] = (char)ch;
        ch = fgetc( stream );
    }
    
    // Add null to end string 
    buffer[i] = '\0';
    printf( "%s\n", buffer );
    fclose( stream );
}

Remark

fgetc reads a single character from the current position of a file associated with stream. The function then increments the associated file pointer (if defined) to point to the next character. If the stream is at end of file, the end-of-file indicator for the stream is set.

See Also

fputc

header to Include

origin.h

Reference