gpt4 book ai didi

c++ - C 是否有任何 setfill() 替代方案?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:12:10 24 4
gpt4 key购买 nike

在 C++ 中:

int main()
{
cout << setfill('#') << setw(10) << 5 << endl;

return 0;
}

输出:

#########5

C 是否有任何 setfill() 替代方案?或者如何在不手动创建字符串的情况下在 C 中执行此操作?

最佳答案

 int x= 5; 
printf("%010d",x);

将输出: 0000000005

现在,如果您真的想要“#”而不是“0”,则必须在字符串中手动替换它们。

也许:

char buf[11], *sp = buf; 
snprintf(buf, 11, "%10d", x);
while( (sp = strchr(sp, ' ')) != '\0'){ *sp = '#'; }
puts(buf);

关于c++ - C 是否有任何 setfill() 替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3867633/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com