Create a function thats like printf?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ajm113
    New Member
    • Jun 2007
    • 161

    Create a function thats like printf?

    I want to create a function that works like printf with the two params like so:

    1. const char*

    2. _Format

    So I can use %s and so forth into the first param just like printf to enter variables to be placed in the message.

    How may I accomplish this?
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    printf is what is known as a variadic function.

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      Search in the net for Variable argument list.
      You shuld use va_arg,va_start etc...
      Start writing the code and if u face issues post again here.

      Raghu

      Comment

      • Ajm113
        New Member
        • Jun 2007
        • 161

        #4
        K I am trying to send this information into a string so I can send it to my edit control, but it seems the program crashes at this line:
        Code:
          len = _vscprintf( message, args ) // _vscprintf doesn't count
                                       + 1; // terminating '\0'

        The debugger isn't much help and I can't find much crash topics on the function...

        Code:
        void PrintLine(const char * message, HWND control, ...)
        {
        stringstream ss;
        
        va_list args;
           int len;
           char * buffer;
        
           va_start( args, message );
           len = _vscprintf( message, args ) // _vscprintf doesn't count
                                       + 1; // terminating '\0'
           buffer = (char*) malloc( len * sizeof(char) );
           vsprintf( buffer, message, args );
        
           ss <<  buffer;
           free( buffer );
        
        SendMessage(control, EM_REPLACESEL ,0,(LPARAM)ss.str().c_str());  
        
        }

        Comment

        • dtimes6
          New Member
          • Oct 2006
          • 73

          #5
          Please take a look at header file cstdarg (stdarg.h)

          Comment

          Working...