Reputation: 3244
I want to have something like a cross platform snprintf
function, so I'm trying to use this (perhaps there are other solutions, but I'm wondering exactly that):
void string_print(char *str, size_t size, const char *format, ...) {
va_list args;
va_start(args, format);
#ifdef _WIN32
sprintf_s(str, size, format, args);
#else
snprintf(str, size, format, args);
#endif
va_end(args);
}
Example of usage:
// timeStepNumber == 1
char fileName[40];
string_print(fileName, 40, "Flow%d.dat", timeStepNumber);
But in this case I have fileName == "Flow-14843.dat"
, although va_arg(args, int) == 1
. Can anybody explain, what maybe wrong in the string_print
function?
Upvotes: 0
Views: 315
Reputation: 6003
As indicated by imbtfab, use vsnprintf()
in place of snprintf()
and _vsnprintf()
in place of sprintf_s
.
Upvotes: 1
Reputation: 493
You need to use vsnprintf/vsnprintf_s functions with vararg lists.
vsnprintf(str, size, format, args);
Upvotes: 1