in my program I use the following function:
void write_log(char *format, ...)
{
va_list arg;
char txt[512];
// Get string
va_start(arg, format);
vsprintf(txt, format, arg); // TODO: txt might overflow
va_end(arg);
// Write to stdout
if(debug_console)
write_console(debug_console, txt);
printf(txt);
}
Is there any safe alternative to vsprintf? I do not want to change the
way I am passing the parameters, because I used this function in
thousands of code lines of a project I've been writing for two years now.
Thank you,
Thomas Rogg
> Is there any safe alternative to vsprintf? I do not want to change the
> way I am passing the parameters, because I used this function in
> thousands of code lines of a project I've been writing for two years now.
Sounds like you want vsnprintf().
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
>Is there any safe alternative to vsprintf?
C99 has vsnprintf, and many non-C99 systems also provide it.
-- Richard
None in C89 AFAIK, but C99 has vsnprintf()
vsprintf(txt, sizeof txt, format, arg);
GNU's libc also has the supremely convenient but nonstandard asprintf()
and vasprintf(), which malloc() the buffer for you. Use only if
portability isn't important.
-Peter
SNPRINTF( BUF, SIZE, FORMAT, ARGS );
IT IS FOUND IN A WINDOW ENVIRONMENT AND LINUX BUT NOT DOS.
THERE MAY BE AN OPEN SOURCE FUNCTION AVAILABLE SOMEWHERE IF YOU DO A
SEARCH IN GOOGLE.COM FOR IT.
I HOPE THIS HELPS. YOU DIDN'T SPECIFY PLATFORM AND COMPILER.
--
Moonie
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------
snprintf or _snprintf is like sprintf
my mistake oops. sorry.