1

I need the target length of how many characters I will need in a *printf function. My scenario is a logging facility that should finally create a string (what happens to it is irrelevant, it must end in a char *).

I currently have this:

void log(char *format, ...)
{
    char *full_message, *message;
    FILE *nullfile;
    va_list args;

    va_start(args, format);
    nullfile = fopen("/dev/null", "w");
    message_len = vfprintf(nullfile, format, args) + 1;
    fclose(nullfile);
    va_end(args);

    va_start(args, format);
    message = malloc(message_len);
    vsnprintf(message, message_len, format, args);
    va_end(args);

    // now do something with the final message
}

That works like a charm, but it seems overly complicated. Is there an easier way? Something that makes more sense.

Additional detail: I finally format the string further and dump it into an OpenSSL BIO, so its not as easy as using vfprinf.

4

2 回答 2

3

一个理智且合理的简化是使用vsnprintf()而不是vfprintf(),因为您只需要一个NULL指针和一个为零的缓冲区大小,而不是一个文件。优雅一些。(别担心,C 中的一个常见习惯用法是两次调用类似的函数:第一次是为了计算缓冲区长度,第二次是为了真正完成工作。)

va_start(args, format);
int message_len = vsnprintf(NULL, 0, format, args) + 1;
va_end(args);

哦,还有……

void log(char *format, ...)

应该

void log(const char *format, ...)

真的。

于 2013-04-15T21:43:29.720 回答
2

这就是 snprintf 的用途:

void log(char *format, ...)
{
    char *message;
    va_list args;

    va_start(args, format);
    message_len = vsnprintf(0, 0, format, args) + 1;
    va_end(args);

    va_start(args, format);
    message = malloc(message_len);
    vsnprintf(message, message_len, format, args);
    va_end(args);

    // now do something with the final message
}
于 2013-04-15T21:45:30.887 回答