Go4Expert

Go4Expert (http://www.go4expert.com/)
-   MFC (http://www.go4expert.com/articles/mfc-tutorials/)
-   -   OutputDebugString with variable number of arguments (http://www.go4expert.com/articles/outputdebugstring-variable-arguments-t871/)

shabbir 8Jun2006 17:52

OutputDebugString with variable number of arguments
 
1 Attachment(s)
Most often a question comes in my mind that why OutputDebugString does not take variable no of arguments like the TRACE or printf function. I dont know the reason why but I have a solution to the problem.

This article is for those OutputDebugString fan's who are annoyed to use the CString and format the string with the variables like
Code: CPP

CString OutMsg;
OutMsg.Format("FUNCTION NAME >> VARIABLE NAME = %d",VAR_NAME);
OutputDebugString(OutMsg);

Here is the substituted function

Code: CPP

void _OutputDebugString(LPCTSTR lpOutputString, ... )
{
   
    va_list argptr;   
   
    va_start( argptr, lpOutputString );           
   
    TCHAR OutMsg[MAX_LENGTH];
    TCHAR format[MAX_LENGTH];
   
    for(int i=0,j=0;lpOutputString[i] != '\0';i++)
    {
        format[j++] = lpOutputString[i];
        // If escape character
        if(lpOutputString[i] == '\\')
        {
            i++;
            continue;
        }
        // if not a substitutal character
        if(lpOutputString[i] != '%')
            continue;
       
        format[j++] = lpOutputString[++i];
        format[j] = '\0';
        switch(lpOutputString[i])
        {
            // string
        case 's':
            {
                char* s = va_arg( argptr, char * );
                sprintf(OutMsg,format,s);
                strcpy(format,OutMsg);
                j = strlen(format);
                strcat(format," ");
                break;
            }
            // character
        case 'c':
            {
                char c = (char) va_arg( argptr, int );
                sprintf(OutMsg,format,c);
                strcpy(format,OutMsg);
                j = strlen(format);
                strcat(format," ");
                break;
            }
            // integer
        case 'd':
            {
                int d = va_arg( argptr, int );
                sprintf(OutMsg,format,d);
                strcpy(format,OutMsg);
                j = strlen(format);
                strcat(format," ");
                break;
            }
        }
    }           
    OutputDebugString(OutMsg);
    va_end( argptr );
}

Download the cpp file and see how it works.

I have taken care of escape character \, string %s, integers %d and characters %c but you can add the floating point numbers and many more.

TEJUS 15Aug2006 15:59

Re: OutputDebugString with variable number of arguments
 
VER NICE,YOU ARE A VERY CLEVER PERSON,I'M ALSO THANKING U FOR REPLYING

TO MY ARTICLE 'FRIENDSHIP CALCULATOR & HANGMAN'.MY NAME IS TEJUS.

I WAS VERY SAD BECAUSE NOONE DIDN'T REPLY TO ME TILL UR REPLY.

I AM STUDYING IN 'VI'(6TH)'IN S.N.PUBLIC SCHOOL ,KOLLAM

shabbir 15Aug2006 16:49

Re: OutputDebugString with variable number of arguments
 
I could not get you which reply you are talking about.

TEJUS 3Nov2006 19:06

Re: OutputDebugString with variable number of arguments
 
just leave it.

BlasterBlang 14Nov2006 12:19

Re: OutputDebugString with variable number of arguments
 
Thanks for the code. but it doesnt find helping in trace.

BlasterBlang 14Nov2006 12:25

Re: OutputDebugString with variable number of arguments
 
but neway ur code have provided much knowledge to reslove problem.

shabbir 14Nov2006 14:03

Re: OutputDebugString with variable number of arguments
 
BlasterBlang, Please confine links to signatures only

aisha.ansari84 5Mar2008 18:51

Re: OutputDebugString with variable number of arguments
 
i m not so clear with it

rahul.mca2001 6Mar2008 13:17

Re: OutputDebugString with variable number of arguments
 
not clear

shabbir 6Mar2008 13:22

Re: OutputDebugString with variable number of arguments
 
Can you clarify what is not clear?


All times are GMT +5.5. The time now is 06:13.