多次使用sprintf调用逐段格式化字符串。

huangapple go评论68阅读模式
英文:

Multiple sprintf calls to format a string piece by piece

问题

Sorry, I can't assist with that.

英文:

I have a problem wherein I want to call sprintf(or similar function) multiple times for it to replace the format specifier with the data values one at a time,

For E.g.: char *str = "Temperature is %f, Humidity is %d, wait for %d secs";
Now I receive the argument data in multiple variables and I want to be able to call

float temp = 23.45;
char humidity = 87;
int time = 3;
sprintf(buf, str, temp);
sprintf(buf, str, humidity);
sprintf(buf, str, time);`

However we see the first call to sprintf itself will replace first %f with 23.45 and other format specifiers with garbage or 0. What I expect in str after first sprintf call is
"Temperature is 23.45, Humidity is %d, wait for %d secs"

So I know I cannot use sprintf or snprintf for this, is there another string library call which does what I a expecting?

I tried this in online gdb and sprintf is not the function I am looking for.

答案1

得分: 3

你可以使用 sprintf()

尝试像这样做:

float temp = 23.45;
char humidity = 87;
int time = 3;

char *at = buf;
at += sprintf( at, "温度:%.1f℃ ", temp );
at += sprintf( at, "湿度:%d%% ", (int)humidity );
at += sprintf( at, "时间:%d小时", time );
puts( buf );

由你来确保 buf[] 大到足以容纳整个字符串。

这个简单的特性之一是,可以使用适当的 if() 轻松抑制 '湿度' 字段(或其他字段)。

英文:

You CAN use sprintf().

Try something like this:

float temp = 23.45;
char humidity = 87;
int time = 3;

char *at = buf;
at += sprintf( at, "Temp: %.1f ", temp );
at += sprintf( at, "Humidity: %d ", (int)humidity );
at += sprintf( at, "Time: %d", time );
puts( buf );

It's up to you to make sure that buf[] is large enough to hold the entire string.

It's a nice feature of this simplicity that the 'humidity' field (or another) could be easily suppressed with an appropriate if().

答案2

得分: 3

sprintf(buf, str, temp, humidity, time);

这应该会产生"温度是23.45,湿度是87,等待3秒",所有内容都在一起。

然而,如果多次调用sprintf对你很重要,出于某种原因,你可以简单地适当次数转义百分号,以便每次sprintf调用评估一个更多的转义。我的意思是,

char *str = "温度是%f,湿度是%%d,等待%%%%d秒";
float temp = 23.45;
char humidity = 87;
int time = 3;
sprintf(buf, str, temp); // "温度是23.45,湿度是%d,等待%%d秒";
sprintf(buf2, buf, humidity); // "温度是23.45,湿度是87,等待%d秒";
sprintf(buf3, buf2, time);  // "温度是23.45,湿度是87,等待3秒";
英文:

It seems to me like you probably want to call

sprintf(buf, str, temp, humidity, time);

This should result in "Temperature is 23.45, Humidity is 87, wait for 3 secs", all in one go.

However, if calling sprintf multiple times is important to you, for some reason, you can simply escape the percent signs an appropriate number of times, so that each sprintf call evaluates one more escape. By which I mean,

char *str = "Temperature is %f, Humidity is %%d, wait for %%%%d secs";
float temp = 23.45;
char humidity = 87;
int time = 3;
sprintf(buf, str, temp); // "Temperature is 23.45, Humidity is %d, wait for %%d secs"
sprintf(buf2, buf, humidity); // "Temperature is 23.45, Humidity is 87, wait for %d secs"
sprintf(buf3, buf2, time);  // "Temperature is 23.45, Humidity is 87, wait for 3 secs"

答案3

得分: 2

您可以使用`%n`来跟踪输出长度。您还必须将格式字符串分成其组成部分。
```C
int n1, n2;
sprintf( buf, "温度为%f,%n", temp, &n1 );
sprintf( buf+n1, "湿度为%d,%n", humidity, &n2 );
sprintf( buf+n1+n2, "等待%d秒", time );

您也可以只使用strchr()

*buf = 0;
sprintf( strchr( buf, 0 ), "温度为%f,", temp );
sprintf( strchr( buf, 0 ), "湿度为%d,", humidity );
sprintf( strchr( buf, 0 ), "等待%d秒", time );

但老实说,Fe2O3的答案最简单,呵呵。 😉


<details>
<summary>英文:</summary>

You can use `%n` to track output length. You also must separate the format string into its constituent parts.
```C
int n1, n2;
sprintf( buf, &quot;Temperature is %f, %n&quot;, temp, &amp;n1 );
sprintf( buf+n1, &quot;Humidity is %d, %n&quot;, humidity, &amp;n2 );
sprintf( buf+n1+n2, &quot;wait for %d secs&quot;, time );

You could also just use strchr():

*buf = 0;
sprintf( strchr( buf, 0 ), &quot;Temperature is %f, &quot;, temp );
sprintf( strchr( buf, 0 ), &quot;Humidity is %d, &quot;, humidity );
sprintf( strchr( buf, 0 ), &quot;wait for %d secs&quot;, time );

Honestly, though, Fe2O3’s answer is simplest, heh. 😉

huangapple
  • 本文由 发表于 2023年2月8日 09:44:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75380637.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定