By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,906 Members | 1,384 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,906 IT Pros & Developers. It's quick & easy.

passing a variable no. of arguments into sprintf (...)

P: n/a
Morning all,

I'm converting my windows exe into a windows service and to get some
log information I'm replacing TRACE with my own Log.

Since some of the Trace statements take a number of arguments I want to
be able to stuff any number of arguments into my log, as in sprintf.

eg Log( "%s %i Hello", "Hi", 23) gives me 122334 !., Hello with the
following code.

void CLog::Log(LPCTSTR lpszData ...)
{
va_list ap;
va_start(ap, lpszData);
char cRtn[256];
sprintf(cRtn, lpszData, ap); //doesn't work
va_end(ap);

//do something useful with cRtn

}

Any suggestions? I'm guessing that 122334 is a pointer...

Nov 10 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Simon L" <si*******@hotmail.com> schrieb im Newsbeitrag
news:11**********************@f14g2000cwb.googlegr oups.com...
Morning all,

I'm converting my windows exe into a windows service and to get some
log information I'm replacing TRACE with my own Log.

Since some of the Trace statements take a number of arguments I want to
be able to stuff any number of arguments into my log, as in sprintf.

eg Log( "%s %i Hello", "Hi", 23) gives me 122334 !., Hello with the
following code.

void CLog::Log(LPCTSTR lpszData ...)
{
va_list ap;
va_start(ap, lpszData);
char cRtn[256];
sprintf(cRtn, lpszData, ap); //doesn't work
va_end(ap);

//do something useful with cRtn

}

Any suggestions? I'm guessing that 122334 is a pointer...


Use vsprintf...

Heinz
Nov 10 '05 #2

P: n/a
ah.... cheers :-)

Nov 10 '05 #3

P: n/a
have you looked into the usage of boost::format over a printf like
approach? it's format strings are compatible to printf (which eases
transition), provide additional functionality and are fully type
checked, and all that for a minor performance cost.

imho the ellipsis should be avoided where possible as finding bugs in
it's usage is very difficult.

you would use

Log(boost::format("%s %i Hello") % "Hi" % 23)

instead of

Log( "%s %i Hello", "Hi", 23)

TRACE calls should be replacable automatically with a proper regex in
your existing code.

http://boost.org/libs/format/

-- peter

Nov 10 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.