Thread: Creating Functions that use Format control Strings

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    53

    Creating Functions that use Format control Strings

    Is it possible to create functions that are similar to scanf in the sense that I can use a format control string to determine how many variables the function will take in?

    eg:
    new_function(const char*, var1, var2, var3,...);

    if so, can anyone point me in the direction of where to look for more information regarding this?
    (Or can anyone tell me where I can sneak a peek at the scanf functions to see how they work...)

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by tzuchan
    Is it possible to create functions that are similar to scanf in the sense that I can use a format control string to determine how many variables the function will take in?

    eg:
    new_function(const char*, var1, var2, var3,...);

    if so, can anyone point me in the direction of where to look for more information regarding this?
    (Or can anyone tell me where I can sneak a peek at the scanf functions to see how they work...)
    Functions using variable numbers of parameters can be coded using the va_start, va_end, and va_arg functions that are declared in the stdio.h header.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    Quote Originally Posted by hk_mp5kpdw
    Functions using variable numbers of parameters can be coded using the va_start, va_end, and va_arg functions that are declared in the stdio.h header.
    Err... Just looked through my stdio.h file...
    Didn't find any of those functions...
    I'm using the mingw32 compiler... Any ideas where else I should try looking?

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    Quote Originally Posted by chrismiceli
    Whee! just what I was looking for, thanks!

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by tzuchan
    Err... Just looked through my stdio.h file...
    Didn't find any of those functions...
    I'm using the mingw32 compiler... Any ideas where else I should try looking?
    Oops, sorry bout that. The required header appear to be stdarg.h or optionally varargs.h.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    This doesn't really do much, but it's a start, anyway:

    Code:
     int sprint(char buffer[], const char * first, ...)
    { 
     const char * fmt = first;
     
     char * b = buffer;
     
     char ch;
     
     va_list vap;
     
     va_start(vap, first);
      
         while(*fmt)
        {
         ch = *fmt++;
         
             if(ch == '%')
            {     
             ch = *fmt++;
                     
                 if(ch == 's')
                {             
                 char * vs = va_arg(vap, char*);
    
                     while(*vs)
                    {
                     *b++ = *vs++;
                    }
                }    
                 else // ...not recognized...
                {
                 --fmt;
                }          
            }
             else // ... just copy...
            {
             *b++ = ch;
            }     
        } 
     
     *b = 0;
     
     va_end(vap);
     
     return b - buffer;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating own, focusable, control
    By _Elixia_ in forum Windows Programming
    Replies: 4
    Last Post: 11-08-2003, 06:21 PM
  2. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  3. Passing & Returning Strings from Functions
    By nisaacs in forum C Programming
    Replies: 1
    Last Post: 01-30-2002, 05:34 AM
  4. Class accessor functions returning strings?
    By Shadow12345 in forum C++ Programming
    Replies: 6
    Last Post: 12-31-2001, 12:48 PM
  5. Using Strings with Functions
    By DJH in forum C Programming
    Replies: 10
    Last Post: 10-19-2001, 04:40 PM