putc、putwc

将字符写入流。

int putc( 
   int c, 
   FILE *stream  
); 
wint_t putwc( 
   wchar_t c, 
   FILE *stream  
);

参数

  • c
    要写入的字符。

  • stream
    指向 FILE 结构的指针。

返回值

返回已写好的字符。 若要指示错误或文件结尾情况,putcputchar 返回 EOF; putwcputwchar 返回 WEOF。 对于所有的四个程序,请使用 ferrorfeof 检测错误或文件结尾。 如果 stream 是 null 指针,则会调用无效参数处理程序,如参数验证 中所述。 如果允许执行继续,则这些函数返回 EOFWEOF 并将 errno设置为 EINVAL

有关这些内容的更多信息以及其他错误代码,请参见 _doserrno、errno、_sys_errlist 和 _sys_nerr

备注

putc 程序写入单个字符 c 到当前位置的输出 stream 。 任何整数可以传递给 putc,但是仅较低的8位被写入。 putchar 程序与putc( c**,stdout )**相同。 对于每个程序,如果读取错误,流的错误指示器会被设置。 putcputchar 分别类似于 fputc_fputchar,但是都以函数和宏实现 (请参见 在函数和宏之间选择)。 putwcputwchar 分别为 putcputchar的宽字符版本. 如果流在 ANSI 模式中打开,putwc 和 putc 会具有相同的行为。 putc 当前不支持输出到 UNICODE 流。

有**_nolock** 后缀的版本是相同的,但它们不能阻止其他线程的干扰。 有关详细信息,请参阅 _putc_nolock, _putwc_nolock

一般文本例程映射

TCHAR.H 例程

未定义 _UNICODE & _MBCS

已定义 _MBCS

已定义 _UNICODE

_puttc

putc

putc

putwc

要求

例程

必需的标头

putc

<stdio.h>

putwc

<stdio.h> 或 <wchar.h>

控制台在 Windows 应用商店 应用程序中不受支持。 与控制台 stdin、stdout 和 stderr 关联的标准流句柄必须重定向,然后 C 运行时函数才可以在 Windows 应用商店 应用程序中使用它们。 有关兼容性的更多信息,请参见兼容性

C 运行时库的所有版本。

示例

// crt_putc.c
/* This program uses putc to write buffer
 * to a stream. If an error occurs, the program
 * stops before writing the entire buffer.
 */

#include <stdio.h>

int main( void )
{
   FILE *stream;
   char *p, buffer[] = "This is the line of output\n";
   int  ch;

   ch = 0;
   /* Make standard out the stream and write to it. */
   stream = stdout;
   for( p = buffer; (ch != EOF) && (*p != '\0'); p++ )
      ch = putc( *p, stream );
}

Output

This is the line of output

.NET Framework 等效项

请参见

参考

流 I/O

fputc、fputwc

getc、getwc