OutputDebugString with variable number of arguments

Discussion in 'MFC' started by shabbir, Jun 8, 2006.

  1. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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:
    CString OutMsg;
    OutMsg.Format("FUNCTION NAME >> VARIABLE NAME = %d",VAR_NAME);
    OutputDebugString(OutMsg);
    Here is the substituted function

    Code:
    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:

  2. TEJUS

    TEJUS New Member

    Joined:
    Aug 6, 2006
    Messages:
    11
    Likes Received:
    1
    Trophy Points:
    3
    Occupation:
    STUDENT
    Location:
    IN KERALA,KOLLAM
    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
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I could not get you which reply you are talking about.
     
  4. TEJUS

    TEJUS New Member

    Joined:
    Aug 6, 2006
    Messages:
    11
    Likes Received:
    1
    Trophy Points:
    3
    Occupation:
    STUDENT
    Location:
    IN KERALA,KOLLAM
    just leave it.
     
  5. BlasterBlang

    BlasterBlang New Member

    Joined:
    Nov 13, 2006
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for the code. but it doesnt find helping in trace.
     
  6. BlasterBlang

    BlasterBlang New Member

    Joined:
    Nov 13, 2006
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    but neway ur code have provided much knowledge to reslove problem.
     
    Last edited by a moderator: Nov 14, 2006
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    BlasterBlang, Please confine links to signatures only
     
  8. aisha.ansari84

    aisha.ansari84 New Member

    Joined:
    Feb 13, 2008
    Messages:
    82
    Likes Received:
    1
    Trophy Points:
    0
    i m not so clear with it
     
  9. rahul.mca2001

    rahul.mca2001 New Member

    Joined:
    Feb 13, 2008
    Messages:
    103
    Likes Received:
    0
    Trophy Points:
    0
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Can you clarify what is not clear?
     
  11. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
  12. shahata

    shahata New Member

    Joined:
    Aug 9, 2008
    Messages:
    1
    Likes Received:
    1
    Trophy Points:
    0
    A much more simple approach:
    Code:
    void _OutputDebugString(LPCTSTR lpOutputString, ... )
    {
        TCHAR OutMsg[MAX_LENGTH];
    
        va_list argptr;
        va_start(argptr, lpOutputString);
    
        wvsprintf(OutMsg, lpOutputString, argptr);
        OutputDebugString(OutMsg);
    
        va_end(argptr);
    }
    
     
    shabbir likes this.
  13. hsm

    hsm New Member

    Joined:
    Jul 12, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    The Download ZIP file is corrupt
     
  14. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I downloaded once again and they worked fine for me
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice