如何打印出破折号或使用fprintf中/ printf的点? [英] How to print out dash or dot using fprintf/printf?
问题描述
截至现在,我使用下面一行与出点的打印
As of now I'm using below line to print with out dot's
fprintf( stdout, "%-40s[%d]", tag, data);
我期待的输出会像下面,
I'm expecting the output would be something like following,
Number of cards..................................[500]
Fixed prize amount [in whole dollars]............[10]
Is this a high winner prize?.....................[yes]
如何使用fprintf中/ printf的打印出破折号或点?
How to print out dash or dot using fprintf/printf?
推荐答案
一个更快的方法:
如果你永远需要预先知道填充的最大数量(这通常是当你像格式化你有一个固定宽度表的情况下),你可以使用一个静态的轧车字符串,只要抓住一大块出来。这将是比调用的printf
或 COUT
在一个循环更快。
If the maximum amount of padding that you'll ever need is known in advance (which is normally the case when you're formatting a fixed-width table like the one you have), you can use a static "padder" string and just grab a chunk out of it. This will be faster than calling printf
or cout
in a loop.
static const char padder[] = "......................"; // Many chars
size_t title_len = strlen(title);
size_t pad_amount = sizeof(padder) - 1 - title_len;
printf(title); // Output title
if (pad_amount > 0) {
printf(padder + title_len); // Chop!
}
printf("[%d]", data);
您甚至可以做到在一个声明中,有信念的跨越:
You could even do it in one statement, with some leap of faith:
printf("%s%s[%d]", title, padder + strlen(title), data);
这篇关于如何打印出破折号或使用fprintf中/ printf的点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!