fgetc、fgetwcfgetc, fgetwc

从流中读取字符。Read a character from a stream.


int fgetc(   
   FILE *stream   
wint_t fgetwc(   
   FILE *stream   


指向 FILE 结构的指针。Pointer to FILE structure.

返回值Return Value

fgetc 返回作为 int 读取的字符或返回 EOF 以指示错误或文件尾。fgetc returns the character read as an int or returns EOF to indicate an error or end of file. fgetwc 将返回对应于字符读取的宽字符(作为 wint_t)或返回 WEOF 以指示错误或文件结尾。fgetwc returns, as a wint_t, the wide character that corresponds to the character read or returns WEOF to indicate an error or end of file. 对于这两个函数,使用 feofferror 来区分错误和文件结尾条件。For both functions, 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. 如果 streamNULL,则 fgetcfgetwc 将调用无效参数处理程序,如参数验证中所述。If stream is NULL, fgetc and fgetwc invoke the invalid parameter handler, as described in Parameter Validation. 如果允许执行继续,则这些功能将 errno 设置为 EINVAL 并返回 EOFIf execution is allowed to continue, these functions set errno to EINVAL and return EOF.


其中每个函数均从与 stream 关联的文件的当前位置读取单个字符。Each of these functions reads a single character from the current position of 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.

fgetc 等效于 getc,但仅作为函数实现,而不是同时作为函数和宏实现。fgetc is equivalent to getc, but is implemented only as a function, rather than as a function and a macro.

fgetwcfgetc 的宽字符版本;它根据 c 是在文本模式还是二进制模式中打开,来将 stream 读取为多字节字符或宽字符。fgetwc is the wide-character version of fgetc; it reads c as a multibyte character or a wide character according to whether stream is opened in text mode or binary mode.

_nolock 后缀的版本相同,只不过它们可能受到其他线程的影响。The versions with the _nolock suffix are identical except that they are not protected from interference by other threads.

有关在文本模式和二进制模式中处理宽字符和多字节字符的详细信息,请参阅文本模式和二进制模式中的 Unicode 流 I/OFor 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 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_fgettc fgetc fgetc fgetwc


函数Function 必需的标头Required header
fgetc <stdio.h><stdio.h>
fgetwc <stdio.h> 或 <wchar.h><stdio.h> or <wchar.h>

有关其他兼容性信息,请参见“简介”中的 兼容性For additional compatibility information, see Compatibility in the Introduction.


// 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:  
   fopen_s( &stream, "crt_fgetc.txt", "r" );  
   if( stream == 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 );  

输入:crt_fgetc.txtInput: crt_fgetc.txt

Line one.  
Line two.  


Line one.  
Line two.  

请参阅See Also

流 I/O Stream I/O
fputc、fputwc fputc, fputwc
getc、getwcgetc, getwc