S
S
Sergey Savostin2014-03-29 14:38:21
C++ / C#
Sergey Savostin, 2014-03-29 14:38:21

Is there a sprintf with an array of format options?

I want something strange...
There is a string like "Something: %s, anything: %d, different: %0.2f", which is obtained from the outside (i.e. the number of format arguments is unknown) and the corresponding size (and types) "array " values ​​to be substituted in the appropriate places.
You need to do "sprintf".
As long as it comes to mind

sprintf(buf, fmt, par[0], par[1], par[2], par[3], par[4], и так далее до X, а если мало, то до Y);

Or is there sprintf in nature, which substitutes only the first parameter, without touching the rest (then it can be done in a loop).
In extreme cases, you can limit yourself to only %s, because the values ​​are still returned as strings.
For a number of reasons, I don’t want to pull Boost (if only because it has long been not just one header file, but a bunch of dependencies, albeit internal ones).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2014-03-29
@jcmvbkbc

There is a string like "Something: %s, anything: %d, miscellaneous: %0.2f", which is obtained from the outside (i.e. the number of format arguments is unknown)

This can become a serious vulnerability because sprintf has, for example, %n writing to memory at the address in the passed parameter.
If you still have C++, maybe std::ostringstream will do?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question