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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17751826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:57:05  来源:igfitidea点击:

Print equal-width columns in C using printf formatting

cprintf

提问by Brian Brown

I would like to print columns using printf in C. I wrote this code:

我想在 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;
}

It works but I have such output:

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

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

So the columns are not equal-width. Basicly, I would like to make it like this, that no matter how long is text in my argument, my function would ALWAYS print out a nice formatted columns. The question is: how can I do this?

所以列不是等宽的。基本上,我想这样做,无论我的论点中的文本有多长,我的函数都会始终打印出一个漂亮的格式化列。问题是:我该怎么做?

By saing niceI meant that no matter how long text I pass to my printing function, it will always print out equal-width columns, for example:

好的我的意思是无论我传递给打印功能的文本有多长,它都会打印出等宽的列,例如:

I have this output that looks like this:

我的输出如下所示:

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  

I want it to look like this (no matter how long my text arguments will be):

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

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    

采纳答案by jh314

You can find the maximum length for txt1, txt2, and txt3, and then format it:

你可以找到的最大长度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);

回答by Michael Burr

If you want the strings to be truncated if they're larger than the column width, then you can just add a precision for the string format specification:

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

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

With that printf(), the output of your example program looks like:

这样printf(),您的示例程序的输出如下所示:

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

回答by Mats Petersson

Unfortunately, there is no TRIVIAL method for doing this.

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

You could do a two-pass method - in main():

你可以做一个两遍的方法 - 在 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]); 

and then this:

然后这个:

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);
}

回答by Paul

Take a look at my simple library, libtprint: https://github.com/wizzard/libtprintThe code is pretty simple, you should be able to understand how it works.

看看我的简单库libtprinthttps://github.com/wizzard/libtprint代码很简单,你应该能够理解它是如何工作的。

Basically what you need is to play with the fields widths for each column and calculate alignment offsets.

基本上你需要的是处理每列的字段宽度并计算对齐偏移量。

Hope it helps !

希望能帮助到你 !