Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用fprintf/printf打印破折号或圆点?_C++_C_Text_Printf - Fatal编程技术网

C++ 如何使用fprintf/printf打印破折号或圆点?

C++ 如何使用fprintf/printf打印破折号或圆点?,c++,c,text,printf,C++,C,Text,Printf,到现在为止,我正在使用下线打印,不带网点 fprintf( stdout, "%-40s[%d]", tag, data); 我希望输出结果如下所示 Number of cards..................................[500] Fixed prize amount [in whole dollars]............[10] Is this a high winner prize?.....................[yes] 卡片数量[500]

到现在为止,我正在使用下线打印,不带网点

fprintf( stdout, "%-40s[%d]", tag, data);
我希望输出结果如下所示

Number of cards..................................[500] Fixed prize amount [in whole dollars]............[10] Is this a high winner prize?.....................[yes] 卡片数量[500] 固定奖金金额[整美元]………[10] 这是高奖吗?……[是]
如何使用fprintf/printf打印破折号或圆点?

您必须自己使用圆点或圆点填充输出字符串

比如(原谅我的C,它生锈了):

printmount(字符*txt,整数金额){
printf(“%s”,txt);

对于(int xa=strlen(txt);xa您不能在一个语句中完成它。 您可以使用sprintf,然后自己用点代替空格,或者 差不多

int chars_so_far;
char padder[40+1]= '..........'; //assume this is 40 dots.
printf("%.40s%n",tag,&chars_so_far);
printf("%s[%d]",padder+chars_so_far,data);

编辑:
基于@Ates的padder概念简化了我上面的示例。这种方式不需要任何关于标记字符串是太大还是太小的“信心飞跃”——它总是从第41列中的数据开始。我建议编写一个函数,用X字符填充字符串,并用它来生成
printf
的第一个参数比如:

char s[40];
pad_str(tag, s, 40, '.');
fprintf( stdout, "%-40s[%d]", s, data);
请注意,示例数据的第三行需要以下格式:

"%-40s[%s]"
更快的方法:

如果预先知道所需的最大填充量(这通常是在格式化固定宽度的表时出现的情况),则可以使用静态“padder”字符串并从中获取一个块。这比在循环中调用
printf
cout
要快

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);
你甚至可以用一句话来表达你的信念:

printf("%s%s[%d]", title, padder + strlen(title), data);

另一个解决方案是使用一个微小的辅助函数

static inline size_t min(size_t a, size_t b)
{
    return a < b ? a : b;
}

这基本上就是Ates发布的内容,但实际上我是自己弄明白的;)

你可以用iostreams而不是printf轻松做到这一点

cout << setw(40) << setfill('.') << left << tag[i] << '[' << data[i] << ']' << endl;

cout我认为有更好的方法

#include <string.h>
#include <stdio.h>

#define MIN(A,B) ((A)<(B)?(A):(B))
char *dashpad(char *buf, int len, const char *instr) {
    memset(buf, '-', len);
    buf[len] = 0;
    int inlen = strlen(instr);
    memcpy(buf, instr, MIN(len, inlen));
    return buf;
}
main() {
    char buf[40];
    printf("%s\n", dashpad(buf, 40, "Hello world, dash padded "));
}
#包括
#包括

#定义MIN(A,B)((A)@Ates:如果你想正确地检查边界,它可以,但我不能不调用两次
strlen()
,因此,你需要缓存这个值->两个语句!如果
strlen(title)>strlen(padder)
@Christoph,你的最后一个建议就被打破了,这就是所谓的“信仰的飞跃”:@Ates:有趣的概念,你的“忠实编程”;如果((int)pad\u amount>0)
将size\u t更改为int,则在
中需要size\u t->int;必须将int传递给printf()函数族中的“*”。或者使帮助程序更有用:printf(“%s%s[%d]”,title,getPadding(title),data);+1以便于阅读和理解,并且在某些情况下(尽管有些不常见)不会中断。首先用pad字符填充整个缓冲区,然后覆盖它们似乎有点浪费……此外,您的缓冲区溢出(难道您不喜欢C)-s/be:buf[len-1]=0;并且您的min()中存在空擦除错误,也应该是len-1。是否可以在printf中调用函数?@Thi:是的,这是合法的-调用函数并将其返回作为参数传递给printf…这是任何过程、OOP或函数语言中的正常编码。这是最有意义的。如果要填充到固定数量的字符,可能需要o切掉太长的标题,因此使用固定宽度数组是有意义的。
cout << setw(40) << setfill('.') << left << tag[i] << '[' << data[i] << ']' << endl;
strstream str;
str << setw(40) << setfill('.') << left << tag[i] << '[' << data[i] << ']' << endl;
printf(%s", str.str());
#include <string.h>
#include <stdio.h>

#define MIN(A,B) ((A)<(B)?(A):(B))
char *dashpad(char *buf, int len, const char *instr) {
    memset(buf, '-', len);
    buf[len] = 0;
    int inlen = strlen(instr);
    memcpy(buf, instr, MIN(len, inlen));
    return buf;
}
main() {
    char buf[40];
    printf("%s\n", dashpad(buf, 40, "Hello world, dash padded "));
}