简体   繁体   English

如何使用变量或重复的占位符在C中构造格式字符串?

[英]How to construct a format string in C with a variable or repeating number of placeholders?

I want to write a hexdump function. 我想编写一个hexdump函数。 Therefore, I want to use sprintf with a format string like 因此,我想将sprintf与格式字符串一起使用

"%c %c %c %c 0x%02X 0x%02X 0x%02X 0x%02X"

and then use this string with characters (or bytes) in printf(). 然后将此字符串与printf()中的字符(或字节)一起使用。

However, I want the user to specify the line width, ie, the number of bytes to be dumped on each line. 但是,我希望用户指定行宽,即每行要转储的字节数。

How do I construct a format string that has the given width, ie a width of 2 results in a format string of "%c %c 0x%02X 0x%02X" ... 如何构造具有给定宽度的格式字符串,即宽度2导致格式字符串为"%c %c 0x%02X 0x%02X" ...

I suggest to do like this: 我建议这样做:

I suggest to do like this: 我建议这样做:

void writeIntegersAsChar(char* str, int* source, int N)
{
    int i;
    for(i = 0; i < N; i ++)
    {
        sprintf(str + i, "%c", source[i]);
    }
}

Instead of constructing a format string using sprintf() only to use later with printf() , consider the following approach which is (basically) two passes over the input string: 与其使用sprintf()构造格式字符串sprintf()仅在以后与printf()一起使用printf()printf()考虑以下方法(基本上)对输入字符串进行两次遍历:

void hex_dump(char *bytes, size_t bytes_len)
{
    const int DUMP_WIDTH = 16;

    for (int i = 0; i < bytes_len; i += DUMP_WIDTH)
    {
        for (int j = 0; j < DUMP_WIDTH; ++j)
        {
            if (i + j >= bytes_len) {
                printf(" ");
                continue;
            }
            char ch = bytes[i + j];
            if (isprint(ch))
            {
                printf("%c", ch);
            }
            else
            {
                printf(".");
            }
        }
        printf("   ");
        for (int j = 0; j < DUMP_WIDTH && i + j < bytes_len; ++j)
        {
            printf("0x%02x", bytes[i + j]);
            printf(" "); 
        }
        printf("\n");
    }
}

The function will output hexdump -like output controlled by the DUMP_WIDTH constant. 该函数将输出hexdump样输出由受控DUMP_WIDTH恒定。 Also, the output is properly aligned. 此外,输出已正确对齐。

It is rather easy to construct such a format string. 构造这样的格式字符串相当容易。

However, the problem is calling sprintf with that format. 但是,问题是使用该格式调用sprintf There is no function in <stdio.h> that would let you pass in an array of arguments. <stdio.h>没有可让您传递参数数组的函数。 Every single printf variant in the standard C requires you to decide the number of arguments at the compile time. 标准C中的每个printf变体都要求您在编译时决定参数数量

While it is possible to pass in less arguments, it doesn't help you with combined hex display such as 尽管可以传递较少的参数,但对于组合的十六进制显示(例如

"%c %c %c %c  0x%02X 0x%02X 0x%02X 0x%02X"

and it gets rather tedious soon. 很快就会变得很乏味。

Unfortunately there is no easy way out - either you need to write your own sprintf variant, or use a loop instead. 不幸的是,没有简单的出路-您需要编写自己的sprintf变体,或者使用循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM