[英]printf() Padding for columns in C
I have a C program that is printing the details of files in the current directory.我有一个 C 程序正在打印当前目录中文件的详细信息。 It is a more specific version of ls.它是更具体的 ls 版本。 I am having trouble getting the columns to be separated by one space character.我无法让列用一个空格字符分隔。 My function is as follows:我的function如下:
while ( (sd = readdir(dir)) != NULL) {
lstat(sd->d_name, &sfile);
printf("%hu", sfile.st_mode);
printf("%d", sfile.st_nlink);
pwd = getpwuid(sfile.st_uid);
printf("%s", pwd->pw_name);
grp = getgrgid(sfile.st_gid);
printf("%s", grp->gr_name);
printf("%lld", sfile.st_size);
t = sfile.st_mtime;
tmp = localtime(&t);
strftime(MY_TIME, sizeof(MY_TIME), "%Y-%m-%d %H:%M", tmp);
printf("%s", MY_TIME);
printf("%s\n", sd->d_name);
I have tried adding %-1s to the printf statements but that only gives me a seg fault.我尝试将 %-1s 添加到 printf 语句中,但这只会给我一个段错误。 If anyone can help me that would be greatly appreciated.如果有人可以帮助我,将不胜感激。 Thank you.谢谢你。
I cannot just add spaces because the columns will not be aligned when you have, for example, file link number > 10 in one column and a single digit link number in the next.我不能只添加空格,因为当您有例如文件链接号 > 10 在一列中并且在下一列中有一个单个数字链接号时,列将不会对齐。
Current output:
16877 5 bobmichaels staff 160 2019-10-30 09:54
16832 23 bobmichaels staff 736 2019-10-29 22:24
Desired output:
16877 5 bobmichaels staff 160 2019-10-30 09:54
16832 23 bobmichaels staff 736 2019-10-29 22:24
Why not simply add a space after each output yourself?为什么不自己在每个 output 之后简单地添加一个空格? Very easy if you combine all the small printf
calls into a single call that prints all:如果您将所有小的printf
调用组合成一个打印所有的调用,则非常容易:
printf("%hu %d %s %s %lld %s %s\n",
sfile.st_mode, sfile.st_nlink, pwd->pw_name, grp->gr_name, sfile.st_size, MY_TIME, sd->d_name);
If you want specific widths of each column, use the field-width specifier of the printf
format, like如果您想要每列的特定宽度,请使用printf
格式的字段宽度说明符,例如
printf("%-4hu %-5d %10s %10s %lld %10s %s\n",
sfile.st_mode, sfile.st_nlink, pwd->pw_name, grp->gr_name, sfile.st_size, MY_TIME, sd->d_name);
[Note that the field widths above are just examples , experiment to get the widths you want] [请注意,上面的字段宽度只是示例,请尝试获得您想要的宽度]
Lastly note that you can never gett columns to align 100% of the time, sooner or later you will get some data that is to long to fit in the width you decided.最后请注意,您永远无法让列在 100% 的时间内对齐,迟早您会得到一些太长以适合您决定的宽度的数据。 The best solution is to check how common it is, and if it's common then adjust the widths, but otherwise there's really no need to bother.最好的解决方案是检查它的常见程度,如果很常见然后调整宽度,否则真的没有必要打扰。
If I understood it right, why not just put the space in the printf, something like:如果我理解正确,为什么不把空格放在 printf 中,比如:
printf(" %s");
if you want to print a column length 10 with right justify:如果你想用右对齐打印列长度 10:
printf("%10s");
if you want to print a column with length 10 left justify:如果要打印长度为 10 的列左对齐:
printf("%-10s");
columns will be padded with whites spaces, if the column is bigger than 10 will push other columns.列将用空格填充,如果列大于 10 将推动其他列。
if u want them to be separated by space just add space after "%lld " or in front of next one if u want all be justify add \t如果您希望它们被空格分隔,只需在“%lld”之后添加空格,或者如果您希望全部合理,则在下一个之前添加空格添加 \t
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.