我正在尝试制作一个可以像这样格式化我的文本输出的 c++ 函数:
#0 编号:80
#1 编号:80
#2 编号:80
#3 编号:80
等...
并且在函数中有一个名为max的参数来限制输出量,例如:
如果max参数设置为10,最多打印/输出10次:
#0 编号:80
#1 编号:80
#2 编号:80
#3 编号:80
#4 编号:80
#5 编号:80
#6 编号:80
#7 编号:80
#8 编号:80
#9 编号:80
#10 编号:80
我试图做的是下面的这段代码,但它并没有像我想要的那样工作:
void format_text(int max){
char Buffer[100];
static int amount;
for (int x = 0; x <= max; x++){
amount ++;
if (max > amount){
length += sprintf(Buffer+length,"#%d id: %d\n", amount, 80);
printf("%s", Buffer);
}
请帮助我实现我为您描述的功能
最佳答案
也许你想要的是一个ostringstream
:
std::string Buffer;
std::ostringstream oss;
//...
oss << "#" << amount << " id: " << 80 << "\n";
//...
Buffer = oss.str();
关于c++ - 以 C++ 方式格式化文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16111375/