是否有任何C标准函数将值“1”传递给所有(%s)

问题描述 投票:-2回答:4

我只想知道是否有任何C(或C ++)标准函数将值“1”传递给所有(%s)。

sprintf(buffer, "record_id(%s)record_num(%s)record_val (%s"), "1"));

而不是传递喜欢和复制“1”:

sprintf(query_buffer, "record_id(%s)record_num(%s)record_val (%s"), "1", "1", "1"));
c++ c
4个回答
4
投票

我想你可以像这样格式化它;

sprintf( buffer, "record_id(%s)record_num(%1$s)record_val (%1$s"), "1") );

这是printf格式字符串的POSIX扩展。您可以使用n $来指示列表中的第n个参数。


1
投票

没有。

没有C标准功能。至于C ++(因为你说cpp函数),我不确定,但我非常怀疑它存在。


0
投票

标准中没有这样的东西。另请查看thisthis以获取更多信息。

你将不得不寻找一个这样做的图书馆。

例如,在C ++中,这可能是提升:

#include <boost/format.hpp>
int main ()
{
    std::cout << boost::format("%1%.%1%.%2%") % 12 % 2014 << '\n';
}

输出12.12.2014


0
投票

没有!但是你可以创建自己的函数来接收一个数字字符串并将其放入%s ...

© www.soinside.com 2019 - 2024. All rights reserved.