fgetc, _fgetchar

Description

Read a character from a stream (fgetc) or stdin (_fgetchar).

#include <stdio.h>

int fgetc( FILE *stream );

int _fgetchar( void );

stream Pointer to FILE structure  

Remarks

The fgetc function reads a single character from the current position of the file associated with stream. The character is converted and returned as an int. The function then increments the associated file pointer (if any) to point to the next character. The _fgetchar function is equivalent to fgetc(stdin).

The fgetc and _fgetchar routines are identical to getc and getchar, but they are functions rather than macros.

Return Value

The fgetc and _fgetchar functions return the character read. They 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.

Compatibility

fgetc

Standards:ANSI, UNIX

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

_fgetchar

Standards:None

16-Bit:DOS, QWIN

32-Bit:DOS32X

See Also

fputc, _fputchar, getc, getchar

Example

/* FGETC.C: This program uses getc to read the first 80 input characters

* (or until the end of input) and place them into a string named buffer.

*/

#include <stdio.h>

#include <stdlib.h>

void main( void )

{

FILE *stream;

char buffer[81];

int i, ch;

/* Open file to read line from: */

if( (stream = fopen( “fgetc.c”, “r” )) == NULL )

exit( 0 );

/* Read in first 80 characters and place them in “buffer”: */

ch = fgetc( stream );

for( i=0; (i < 80 ) && ( feof( stream ) == 0 ); i++ )

{

buffer[i] = ch;

ch = fgetc( stream );

}

/* Add null to end string */

buffer[i] = '\0';

printf( “%s\n”, buffer );

fclose( stream );

}

Output

/* FGETC.C: This program uses getc to read the first 80 input characters

/* (or