Archived

This topic is now archived and is closed to further replies.

Ace17

Wrapper for wsprintf function

Recommended Posts

I''m planning to write a logger class I need a function like this : void Output(char* s, ...) { char buffer[256]; wsprintf(buffer, s, ...); fwrite(buffer, 1, strlen(buffer), file); } Use like this : Output("value = %d ", value); Output("%d %d %d", int(1), int(2), int(3)); The syntax isn''t correct, of course. So if somebody knows the right syntax... Thanx

Share this post


Link to post
Share on other sites
look for wvsprintf and va_arg, va_end and va_start in the MSDN.
Here is an example how to handle the variable argument;
  
void _Trace (LPCTSTR lpszFormat, ...)
{
va_list argList;
TCHAR szStrBuffer[1024];

va_start (argList, lpszFormat);
_vsntprintf (szStrBuffer, sizeof (szStrBuffer) / sizeof (szStrBuffer[0]), lpszFormat,
argList);
OutputDebugString (szStrBuffer);
OutputDebugString (_T("\n"));
va_end (argList);
}


Edited by - VolkerG on October 20, 2001 2:04:45 PM

Share this post


Link to post
Share on other sites
If you are using C++, you should know that the STL has something call a string stream that can do this very nicely.




#include


...

std::ostringstream os;

int j = 3;

float v = 2;


os << "my int value is : " << j
<< ". My float value is : " << v;

std::string theStringFromTheOs = os.str();

Share this post


Link to post
Share on other sites