fgetc、fgetwcfgetc, fgetwc

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

语法Syntax

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

参数Parameters

stream
指向文件结构的指针。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. 如果NULLfgetcfgetwc中所述将调用无效参数处理程序,参数验证If stream is NULL, fgetc and fgetwc invoke the invalid parameter handler, as described in Parameter Validation. 如果允许执行继续,则这些函数将设置errnoEINVAL并返回EOFIf execution is allowed to continue, these functions set errno to EINVAL and return EOF.

备注Remarks

其中每个函数从与关联的文件的当前位置读取单个字符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.

fgetwc是宽字符版本的fgetc; 它读取c作为多字节字符或宽字符根据是否中打开文本模式还是二进制模式。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_fgettc fgetcfgetc fgetcfgetc fgetwcfgetwc

要求Requirements

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

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.

示例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:
   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.

输出Output

Line one.
Line two.

请参阅See also

流 I/OStream I/O
fputc、fputwcfputc, fputwc
getc、getwcgetc, getwc