Advertisement

parsing '...' as in sprintf(,,...) possible?

Started by May 03, 2001 01:57 AM
1 comment, last by SikCiv 23 years, 9 months ago
Instead of having excess code as followed..
  
// Repeated Code through out my files.

szTemp[128];
sprintf(szTemp,"Value1 %d Value2 %d",value1, value2);
DebugOutput(szTemp);
  
Is it possible to mimick the 3 lines into one line and a seperate function...
  

// My New function

DebugPrintf("Value1 %d Value2 %d",value1, value2);




// In my Library...

void DebugPrintf(const char *format, ... )
{
    char szTemp[MAX_PATH];


    // The below line doesnt work off course

    //

    // How do I parse ... from above into sprintf below?

    //

    sprintf(szTemp,format,...);  
    DebugOutput(szTemp);

}


  

  Downloads:  ZeroOne Realm

try this code. it works for me. I didn''t come up with it, but saw it on a tutorial on gamasutra I think. It writes the text out to a file using vfprintf, but i''m sure there is a version of sprintf (maybe vsprintf) that works like this:

BOOL text(char *lpszText, ...) {

va_list argList;
FILE *pFile;
va_start(argList, lpszText);

if ((pFile = fopen("log.txt", "a+")) == NULL)
return(FALSE);

vfprintf(pFile, lpszText, argList);
putc(''\n'', pFile);
fclose(pFile);
va_end(argList);

return(TRUE);
}


then you can say:

int anInt = 10;
float aFloat = 20.0f;
char *aString = "testing";

text("Testing text: %d %f %s", anInt, aFloat, aString);

or whatever.

kiev
Advertisement
in order to parse that you use a macro
like kiev_class showed you, that''s the "trick"

Arkon
[QSoft Systems]

This topic is closed to new replies.

Advertisement