简体   繁体   English

以C ++方式格式化文本

[英]Format text in c++ way

I'm trying to make a c++ function that can format my text output like this: 我正在尝试制作一个可以格式化文本输出的c ++函数,如下所示:

#0 id: 80 #0 id:80

#1 id: 80 #1 ID:80

#2 id: 80 #2 id:80

#3 id: 80 #3 id:80

etc... 等等...

and have a parameter called max in the function to limit the amount of output, like: 并在函数中具有一个名为max的参数以限制输出量,例如:

if the max parameter was set to 10, it most print/output only 10 times: 如果将max参数设置为10,则最多仅打印/输出10次:

#0 id: 80 #0 id:80

#1 id: 80 #1 ID:80

#2 id: 80 #2 id:80

#3 id: 80 #3 id:80

#4 id: 80 #4 id:80

#5 id: 80 #5 id:80

#6 id: 80 #6 ID:80

#7 id: 80 #7 id:80

#8 id: 80 #8 id:80

#9 id: 80 #9 ID:80

#10 id: 80 #10 ID:80

What I have tried to do is this code down below, but it doesn't work as I wanted: 我试图做的是下面的这段代码,但是它不能按我的意愿工作:

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);

}

Please help me in making a function as I have described for you 请帮助我完成我为您描述的功能

Perhaps what you are after is an ostringstream : 也许您追求的是一个ostringstream

std::string Buffer;
std::ostringstream oss;

//...
    oss << "#" << amount << " id: " << 80 << "\n";
//...

Buffer = oss.str();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM