【问题标题】:How to nest fprintf in another function如何将 fprintf 嵌套在另一个函数中
【发布时间】:2015-02-07 16:13:58
【问题描述】:

我想写一个类似的函数

void put(const char *label, const char *format, ...)
{
    /* There is a file f open for writing */
    fprintf(f, "%s:\n", label);
    fprintf(f, format);
}

... 并使用它:

put("My Label: ", "A format with number in it %i", 3);

我尝试这样做,但我的文件中得到的是 16711172 而不是 3

【问题讨论】:

标签: c printf variadic-functions


【解决方案1】:

试试下面的代码:

#include<stdio.h>
#include <stdarg.h>

void put(const char *label, const char *format, ...)
{
    va_list         arg_ptr;

    va_start(arg_ptr, format);
    /* There is a file f open for writing */
    fprintf(stdout, "%s: ", label);
    vfprintf(stdout, format, arg_ptr);
    va_end(arg_ptr);
}

int main() {
    put("LAB", "%d\n", 5);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-11
    • 2021-08-01
    • 1970-01-01
    • 2021-06-08
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多