fputc、fputwcfputc, fputwc

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

语法Syntax

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

参数Parameters

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

stream
指向文件结构的指针。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. 如果NULL,这些函数将调用无效参数处理程序,如中所述参数验证If stream is NULL, these functions invoke the invalid parameter handler, as described in Parameter Validation. 如果允许执行继续,它们将返回EOF并设置errnoEINVALIf 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.

备注Remarks

每个函数将单个字符写入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,与文件关联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 宽字符版本fputcWide-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

要求Requirements

函数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. 与控制台关联的标准流句柄 —stdinstdout,并stderr— 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.

示例Example

// 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