473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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
3 2047
"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
ah.... cheers :-)

Nov 10 '05 #3
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
6
by: Max | last post by:
Last time I tried to explain this on another forum it didn't go too well, so I'll try my best and if you know what I'm talking about then please tell me how to do this. I have a class, inside I...
10
by: Janus | last post by:
Hi, Is there a way to pass arguments to the callback function used inside an addEventListener? I see that I can only list the name of the callback function. For eg, I use this: var...
4
by: Punkie | last post by:
When having optional function arguments, the extra arguments are accessed via an av_list structure. While traversing is trivial, changing the value isnt. <code> void my_sprintf (char *dest,...
6
by: junky_fellow | last post by:
Hi, I want to define a function log_file(), that would print the formatted message to stdout if the FILE pointer passed is NULL else print the formatted output to the file corresponding to...
18
by: squaretriangle | last post by:
Is it possible through some trickery, perhaps using the methods of stdarg, to pass a dynamic (unknown at compile time) number of arguments to a function? For example, I want to enumerate an array...
9
by: oldyork90 | last post by:
I'm going thru code and have never seen this before http://www.webreference.com/programming/javascript/mk/column2/3.html Look at function CreateDragContainer() on line 25. It has no arguments...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.