fgetc, fgetwc, _fgetchar, _fgetwchar
Read a character from a stream (fgetc, fgetwc) or stdin (_fgetchar, _fgetwchar).
int fgetc( FILE *stream ); wint_t fgetwc( FILE *stream ); int _fgetchar( void ); wint_t _fgetwchar( void );
Parameter
- stream
- Pointer to FILE structure.
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.
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 |
Requirements
| Function | Required header | Compatibility |
|---|---|---|
| fgetc | <stdio.h> | ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP |
| fgetwc | <stdio.h> or <wchar.h> | ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP |
| _fgetchar | <stdio.h> | Win 98, Win Me, Win NT, Win 2000, Win XP |
| _fgetwchar | <stdio.h> or <wchar.h> | Win 98, Win Me, Win NT, Win 2000, Win XP |
For additional compatibility information, see Compatibility in the Introduction.
Libraries
All versions of the C run-time libraries.
Example
// crt_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>
int main( void )
{
FILE *stream;
char buffer[81];
int i, ch;
/* Open file to read line from: */
if( (stream = fopen( "crt_fgetc.txt", "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 );
}
Input: crt_fgetc.txt
Line one. Line two.
Output
Line one. Line two.
See Also
Stream I/O Routines | fputc | getc | Run-Time Routines and .NET Framework Equivalents