fgetc, fgetwc, _fgetchar, _fgetwchar

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

intfgetc(FILE*stream);

wint_tfgetwc(FILE*stream);

int_fgetchar(void);

wint_t_fgetwchar(void);

Function Required Header Compatibility
fgetc <stdio.h> ANSI, Win 95, Win NT
fgetwc <stdio.h> or <wchar.h> ANSI, Win 95, Win NT
_fgetchar <stdio.h> Win 95, Win NT
_fgetwchar <stdio.h> or <wchar.h> Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

fgetc and _fgetchar return the character read as an int or return EOF to indicate an error or end of file. fgetwc and _fgetwchar return, as a wint_t, the wide character that corresponds to the character read or return WEOF to indicate an error or end of file. For all four functions, use feof or ferror to distinguish between an error and an end-of-file condition. For fgetc and fgetwc, if a read error occurs, the error indicator for the stream is set.

Parameter

stream

Pointer to FILE structure

Remarks

Each of these functions reads a single character from the current position of a file; in the case of fgetc and fgetwc, this is the 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. Routine-specific remarks follow.

Routine Remarks
fgetc Equivalent to getc, but implemented only as a function, rather than as a function and a macro.
fgetwc Wide-character version of fgetc. Reads c as a multibyte character or a wide character according to whether stream is opened in text mode or binary mode.
_fgetchar Equivalent to fgetc( stdin ). Also equivalent to getchar, but implemented only as a function, rather than as a function and a macro. Microsoft-specific; not ANSI-compatible.
_fgetwchar Wide-character version of _fgetchar. Reads c as a multibyte character or a wide character according to whether stream is opened in text mode or binary mode. Microsoft-specific; not ANSI-compatible.

For more information about processing wide characters and multibyte characters in text and binary modes, see Unicode Stream I/O in Text and Binary Modes.

Generic-Text Routine Mappings

TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_fgettc fgetc fgetc fgetwc
_fgettchar fgetchar fgetchar _fgetwchar

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] = (char)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

Stream I/O Routines

See Also   fputc, getc