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.

指向文件结构的指针。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. 对于fputwcWEOF的返回值指示错误。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,该文件与stream关联。In 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
fputcfputc 等效于putc,但仅作为函数实现,而不是作为函数和宏实现。Equivalent to putc, but implemented only as a function, rather than as a function and a macro.
fputwcfputwc Fputc的宽字符版本。Wide-character version of fputc. 根据是在文本模式还是二进制模式中打开,将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_fputtc fputcfputc fputcfputc fputwcfputwc


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

通用 Windows 平台 (UWP) 应用中不支持控制台。The console is not supported in Universal Windows Platform (UWP) apps. 与控制台(stdinstdoutstderr)关联的标准流句柄必须重定向,然后 C 运行时函数才能在 UWP 应用中使用它们。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 UWP 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/OStream I/O
fgetc、fgetwcfgetc, fgetwc
putc、putwcputc, putwc