varargs打印所有参数

时间:2012-05-07 13:29:11

标签: c++ variadic-functions

如果我有这个功能:

printAll(const char *message, ...)
{
    va_list argptr = NULL;
    va_start(argptr, message);

    // todo: how to printf all the arguments in the message?   

    va_end(argptr);    
}

假设我像这样调用函数:

printAll("My info: Value1 = %d, Value 2=%d", 1, 2);

在这一行:// todo:如何printf消息中的所有参数?

如何打印它们以便:

My info: Value1 = 1, Value 2=2

提前致谢。

3 个答案:

答案 0 :(得分:15)

您正在寻找旨在实现此目的的vprintf()功能:

vprintf(message, argptr);

v*printf()系列函数的工作方式与普通函数基本相同,只是它们采用va_list而不是varargs。他们不会为你打电话va_end(),所以你现在的方式是正确的。

答案 1 :(得分:1)

这是一般想法的草图(虽然已完成的版本,必须处理更多的事情,如字段宽度,精度,更多转换等。

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

int my_vfprintf(FILE *file, char const *fmt, va_list arg) {

    int int_temp;
    char char_temp;
    char *string_temp;
    char ch;
    int length = 0;

    char buffer[512];

    while ( ch = *fmt++) {
        if ( '%' == ch ) {
            switch (ch = *fmt++) {
                /* %% - print out a single %    */
                case '%':
                    fputc('%', file);
                    length++;
                    break;

                /* %c: print out a character    */
                case 'c':
                    char_temp = va_arg(arg, int);
                    fputc(char_temp, file);
                    length++;
                    break;

                /* %s: print out a string       */
                case 's':
                    string_temp = va_arg(arg, char *);
                    fputs(string_temp, file);
                    length += strlen(string_temp);
                    break;

                /* %d: print out an int         */
                case 'd':
                    int_temp = va_arg(arg, int);
                    itoa(int_temp, buffer, 10);
                    fputs(buffer, file);
                    length += strlen(buffer);
                    break;

                /* %x: print out an int in hex  */
                case 'x':
                    int_temp = va_arg(arg, int);
                    itoa(int_temp, buffer, 16);
                    fputs(buffer, file);
                    length += strlen(buffer);
                    break;
            }
        }
        else {
            putc(ch, file);
            length++;
        }
    }
    return length;
}

int my_printf(char const *fmt, ...) {
    va_list arg;
    int length;

    va_start(arg, fmt);
    length = my_vfprintf(stdout, fmt, arg);
    va_end(arg);
    return length;
}

int my_fprintf(FILE *file, char const *fmt, ...) {
    va_list arg;
    int length;

    va_start(arg, fmt);
    length = my_vfprintf(file, fmt, arg);
    va_end(arg);
    return length;
}


#ifdef TEST 

int main() {
    my_printf("%s", "Some string");
    return 0;
}

#endif

答案 2 :(得分:1)

这是一个printAll(),它将完全按照您的意愿执行...

#ifdef __cplusplus
#include <cstring>
#include <cstdio>
#include <cstdarg>
using namespace std;
#else
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#endif



void printAll(const char* fmt, ...) {
    va_list ap;
    va_start(ap, fmt);
    const size_t MAX_LEN = 1023;
#ifdef __cplusplus
    char msg[MAX_LEN+1] = {0};
#else
    char msg[MAX_LEN+1];
    for (int i = 0; i < MAX_LEN+1; i++)
    {
       msg[i] = 0;
    }
#endif
    vsnprintf(msg, MAX_LEN, fmt, ap);
    printf("%s\n", msg);
    va_end(ap);
}

int main() {
    printAll("My info: Value1 = %d, Value 2=%d", 1, 2);
    return 0;
}

我的信息:值1 = 1,值2 = 2