fputc、fputwcfputc, fputwc

将字符写入流。Writes a character to a stream.


int fputc(  
   int c,  
   FILE *stream   
wint_t fputwc(  
   wchar_t c,  
   FILE *stream   


要写入的字符。Character to be written.

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

返回值Return Value

其中每个函数都会返回写入的字符。Each of these functions returns the character written. 对于 fputc,返回值 EOF 指示一个错误。For fputc, a return value of EOF indicates an error. 对于 fputwc,返回值 WEOF 指示一个错误。For fputwc, a return value of WEOF indicates an error. 如果 streamNULL,这些函数则会调用无效的参数处理程序,如参数验证中所述。If stream is NULL, these functions invoke the invalid parameter handler, as described in Parameter Validation. 如果允许继续执行,这些函数则返回 EOF,并将 errno 设置为 EINVALIf execution is allowed to continue, they return EOF and set errno to EINVAL.

有关这些代码以及其他错误代码的详细信息,请参阅 _doserrno、errno、_sys_errlist 和 _sys_nerrSee _doserrno, errno, _sys_errlist, and _sys_nerr for more information on these, and other, error codes.


其中每个函数将单个字符 c 在关联文件位置指示器(如果已定义)所指示的位置写入文件,并根据情况提升指示器。Each of these functions writes the single character c to a file at the position indicated by the associated file position indicator (if defined) and advances the indicator as appropriate. 情况下fputcfputwc,与关联文件streamIn the case of fputc and fputwc, the file is associated with stream. 如果文件不支持定位请求,或在追加模式中打开,则该字符将被追加到流的末尾。If the file cannot support positioning requests or was opened in append mode, the character is appended to the end of the stream.

如果在 ANSI 模式下打开流,则这两个函数行为相同。The two functions behave identically if the stream is opened in ANSI mode. fputc 当前不支持到 UNICODE 流中的输出。fputc does not currently support output into a UNICODE stream.

_nolock 后缀的版本相同,只不过它们可能受到其他线程的影响。The versions with the _nolock suffix are identical except that they are not protected from interference by other threads. 有关详细信息,请参阅 _fputc_nolock、_fputwc_nolockFor more information, see_fputc_nolock, _fputwc_nolock.

下面是例程特定的备注。Routine-specific remarks follow.

例程所返回的值Routine 备注Remarks
fputc 等效于 putc,但仅作为函数实现,而不是同时作为函数和宏实现。Equivalent to putc, but implemented only as a function, rather than as a function and a macro.
fputwc fputc 的宽字符版本。Wide-character version of fputc. 根据 stream 是在文本模式还是二进制模式中打开,将 c 编写为多字节字符或宽字符。Writes c as a multibyte character or a wide character according to whether stream is opened in text mode or binary mode.

一般文本例程映射Generic-Text Routine Mappings

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_fputtc fputc fputc fputwc


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

控制台在 Windows 8.x 应用商店Windows 8.x Store 应用程序中不受支持。The console is not supported in Windows 8.x 应用商店Windows 8.x Store apps. 与控制台 stdinstdoutstderr 关联的标准流句柄必须重定向,然后 C 运行时函数才可以在 Windows 8.x 应用商店Windows 8.x Store 应用中使用它们。The standard stream handles that are associated with the console—stdin, stdout, and stderr—must be redirected before C run-time functions can use them in Windows 8.x 应用商店Windows 8.x Store apps. 有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.


// crt_fputc.c  
// This program uses fputc  
// to send a character array to stdout.  

#include <stdio.h>  

int main( void )  
   char strptr1[] = "This is a test of fputc!!\n";  
   char *p;  

   // Print line to stream using fputc.   
   p = strptr1;  
   while( (*p != '\0') && fputc( *(p++), stdout ) != EOF ) ;  

This is a test of fputc!!  

请参阅See Also

流 I/O Stream I/O
fgetc、fgetwc fgetc, fgetwc
putc、putwcputc, putwc