【问题标题】:Print equal-width columns in C using printf formatting使用 printf 格式在 C 中打印等宽列
【发布时间】:2013-07-19 17:11:19
【问题描述】:

我想在 C 中使用 printf 打印列。我编写了以下代码:

#include <stdio.h>

void printme(char *txt1, char *txt2, char *txt3)
{
    printf("TXT1: %9s TXT2 %9s TXT3 %9s\n", txt1, txt2, txt3);
}


int main()
{
    printme("a","bbbbbbbeeeeebbbbb","e");
    printme("aaaaaaaa","bbbbbbbbbbbb","abcde");
    return 0;
}

它有效,但我有这样的输出:

TXT1:         a TXT2 bbbbbbbeeeeebbbbb TXT3         e
TXT1:  aaaaaaaa TXT2 bbbbbbbbbbbb TXT3     abcde

所以列不等宽。基本上,我想让它像这样,无论我的参数中的文本有多长,我的函数总是会打印出一个很好的格式化列。问题是:我该怎么做?

通过 nice 我的意思是,无论我传递给我的打印函数多长的文本,它总是会打印出等宽的列,例如:

我的输出如下所示:

a         cd`           fg           ij  
a         cd             fg           ij  
a         cd             fg           ij  
ab         cd             fg           ij  
ab         cd             fg           i j   
ab         cd             fg           ij  
ab         cd             fg           ij  
ab         cde             fgh         ij  
ab         cde             fgh         ij  

我希望它看起来像这样(无论我的文本参数有多长):

a         cd`           fg           ij  
a         cd            fg           ij  
a         cd            fg           ij  
ab        cd            fg           ij  
ab        cd            fg           ij   
ab        cd            fg           ij  
ab        cd            fg           ij  
ab        cde           fgh          ij  
ab        cde           fgh          ij    

【问题讨论】:

标签: c printf


【解决方案1】:

如果您希望字符串大于列宽时被截断,那么您只需为字符串格式规范添加一个精度:

printf("TXT1: %9.9s TXT2 %9.9s TXT3 %9.9s\n", txt1, txt2, txt3);

使用 printf(),示例程序的输出如下所示:

TXT1:         a TXT2 bbbbbbbee TXT3         e
TXT1:  aaaaaaaa TXT2 bbbbbbbbb TXT3     abcde

【讨论】:

    【解决方案2】:

    你可以找到txt1txt2txt3的最大长度,然后格式化:

    // compute the max string length of txt1 inputs in advance
    int s1 = strlen(firstTxt1);
    if (s1 < strlen(secondTxt1)
        s1 = strlen(secondTxt1);
    ...
    
    printf("%.*s %.*s %.*s\n", s1, txt1, s2, txt2, s3, txt3);
    

    【讨论】:

    • 如果您有连续输出(例如,连续将信息打印到类似表格的结构中)并且突然一列的数据比其所有前辈长(需要使列更宽),会发生什么情况?我知道\b\r,但它们并没有真正解决这个问题。
    • 也许是我的编译器,但为什么这个方法对我不起作用? printf("%.*s",5,"hi"); 只是打印“hi”,就好像没有指定长度但 %5s 工作正常。您是否有任何链接可以详细说明这一点?我假设 Google 搜索词是“在 printf 中格式化”?
    【解决方案3】:

    不幸的是,没有 TRIVIAL 方法可以做到这一点。

    您可以执行两遍方法 - 在 main() 中:

    char **data[] = { { "a","bbbbbbbeeeeebbbbb","e" }, 
                      {"aaaaaaaa","bbbbbbbbbbbb","abcde" } };
    
    
    get_columwidths(data[0][0], data[0][1], data[0][2]); 
    get_columwidths(data[1][0], data[1][1], data[1][2]); 
    
    printme(data[0][0], data[0][1], data[0][2]); 
    printme(data[1][0], data[1][1], data[1][2]); 
    

    然后是这个:

    int columnwidths[3];
    
    void get_columwidths(const char *s1, const char *s2, const char *s3)
    {
        int len1 = strlen(s1); 
        int len2 = strlen(s2); 
        int len3 = strlen(s3); 
    
        if (columnwidths[0] < len1) columnwidths[0] = len1;
        if (columnwidths[1] < len2) columnwidths[1] = len2;
        if (columnwidths[2] < len3) columnwidths[2] = len3;
    }
    
    void printme(char *txt1, char *txt2, char *txt3)
    {
        printf("TXT1: %*s TXT2 %*s TXT3 %*s\n", 
               columnwidths[0], txt1, columnwidths[1], txt2, columnwidths[2], txt3);
    }
    

    【讨论】:

    • +1 因为使用%*s 而不是%.*s(注意句点),就像在接受的答案中一样,此方法在打印整数时打印前导零(@987654325 @)。
    【解决方案4】:

    看看我的简单库,libtprint:https://github.com/wizzard/libtprint 代码很简单,你应该能理解它的工作原理。

    基本上,您需要使用每列的字段宽度并计算对齐偏移量。

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 2014-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多