约 1155 字

约 6 分钟

#C 语言标准库函数 fprintf

#include <stdio.h>

int fprintf(FILE *restrict stream, const char *restrict format, ...);

说明

按格式向指定的文件流写出数据。

按照格式说明提供参数,转换以当前 locale 的相应规则为准。

自 C89 起提供。

参数

  • stream - 参与输入输出的有效文件流。
  • format - 以空字符结束的格式串,包含普通字符和转换指令。
  • ... - 随转换指令提供的输出值。

返回值

  • 正常返回实际写出字节数,排除终止符。输出或编码错误时返回负值。

#示例

#include <stdio.h>

int main(void)
{
    FILE *stream = tmpfile();
    if (stream == NULL) {
        return 1;
    }
    int count = fprintf(stream, "item %d\n", 42);
    int close_status = fclose(stream);
    return count != 8 || close_status != 0;
}

#相关接口

#参考标准

更新: 2026/11/28

作者: planc

创建: 2026/11/28