OutputDebugString with variable number of arguments

shabbir's Avatar author of OutputDebugString with variable number of arguments
This is an article on OutputDebugString with variable number of arguments in MFC.
Rated 5.00 By 1 users
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.
Attached Files
File Type: zip OutputDebugString.zip (666 Bytes, 90 views)
TEJUS's Avatar, Join Date: Aug 2006
Go4Expert Member
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's Avatar, Join Date: Jul 2004
Go4Expert Founder
I could not get you which reply you are talking about.
TEJUS's Avatar, Join Date: Aug 2006
Go4Expert Member
just leave it.
BlasterBlang's Avatar, Join Date: Nov 2006
Go4Expert Member
Thanks for the code. but it doesnt find helping in trace.
BlasterBlang's Avatar, Join Date: Nov 2006
Go4Expert Member
but neway ur code have provided much knowledge to reslove problem.

Last edited by shabbir; 14Nov2006 at 14:02.. Reason: Confine links to signatures only
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
BlasterBlang, Please confine links to signatures only
aisha.ansari84's Avatar, Join Date: Feb 2008
Contributor
i m not so clear with it
rahul.mca2001's Avatar, Join Date: Feb 2008
Ambitious contributor
not clear
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Can you clarify what is not clear?