使用printf格式在C中打印等宽列

使用printf格式在C中打印等宽列,c,printf,C,Printf,我想使用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","

我想使用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;
}
因此,列的宽度不相等。基本上,我想这样做,不管我的参数中文本有多长,我的函数都会打印出一个很好的格式化列。问题是:我如何才能做到这一点

saing 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  
我希望它看起来像这样(无论我的文本参数有多长):


您可以找到
txt1
txt2
txt3
的最大长度,然后将其格式化:

// 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);
//提前计算txt1输入的最大字符串长度
int s1=strlen(firstTxt1);
if(s1
不幸的是,没有简单的方法可以做到这一点

您可以在main()中执行两次传递方法-

然后这个:

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);
}
int列宽[3];
void get_列宽(常量字符*s1、常量字符*s2、常量字符*s3)
{
int len1=strlen(s1);
int len2=strlen(s2);
int len3=strlen(s3);
如果(columnwidths[0]
如果要在字符串大于列宽时截断字符串,则只需为字符串格式规范添加精度:

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

看看我的简单库: 代码非常简单,您应该能够理解它是如何工作的

基本上,您需要处理每列的字段宽度并计算对齐偏移


希望有帮助!

在nice格式的列中定义nice。因此,请提前扫描文本,找出最长的字符串,并使用该长度作为列填充值。可能是连续输出的重复,也可能是连续输出的重复(例如,将信息连续打印到类似表格的结构中)突然间,一列的数据比以前的所有列都要长(列需要更宽)?我知道
\b
\r
但它们并没有真正解决这个问题。也许是我的编译器,但为什么这个方法对我不起作用?
printf(“%s”,5,“hi”);
只是打印“hi”好像没有指定长度,但是
%5s
可以正常工作。你有没有详细解释这一点的链接?我假设谷歌搜索词是“printf格式”?+1,因为使用
%*s
而不是
%.*s
(注意句点)与接受的答案类似,此方法在打印整数(
%*d
)时不打印前导零。
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);
}
printf("TXT1: %9.9s TXT2 %9.9s TXT3 %9.9s\n", txt1, txt2, txt3);
TXT1:         a TXT2 bbbbbbbee TXT3         e
TXT1:  aaaaaaaa TXT2 bbbbbbbbb TXT3     abcde