Thread: Problem with sscanf

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    Question Problem with sscanf

    Hello all,

    I am a newbie to this website. Please help me out.

    I have to use a function similar to sscanf (if not the same).

    The requirement for sscanf is :

    int sscanf ( const char * str, const char * format, ...);

    eg. (string,"%d %d",&a,&b);

    Here, i know the number of arguments (2 in this example) that are present. But I need to use it for variable number of arguments.
    The exact requirement is that the 3rd argument specifies how many more arguments do I need to scan. So they are kind of dependent.

    Please help me out as soon as possible..!!

    Thank You..!!
    Saurabh

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can read the first three input items, then use a loop to read the rest according to the 3rd input item.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    Re: Problem with sscanf

    For variable number of arguments you can use ... (ellipsis notation ) in function declaration and definition.

    And for getting those argument you can read about these functions and get to know that,

    Code:
    #include <stdarg.h>
    
           void va_start(va_list ap, last);
           type va_arg(va_list ap, type);
           void va_end(va_list ap);
           void va_copy(va_list dest, va_list src);
    Actually, scanf and printf also uses this mechanism to use variable number of arguments in a function.

    For this,
    "The exact requirement is that the 3rd argument specifies how many more arguments do I need to scan. So they are kind of dependent."
    You can have like this mysscanf(const char * str, const char * format, ..., int argc);

    or you can have like main function main(int argc, char **argv);

    It is also getting variable number of arguments and count for that arguments. But it will be stored like strings. If you want other types you need to convert that into that data type.
    Last edited by sganesh; 03-07-2010 at 11:00 PM.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    Hope this example code will help you
    Code:
    int example(const char *str,...)
    {
    
            int i;
    
            int num_count=atoi(str);
    
            int *num=(int *)&str;
    
            for(i=1;i<=num_count;i++)
                    printf("%d ",*(num+i));
    }
    int main()
    {
    
    example("3",10,20,30);
    }

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    2
    Thanks everyone for the replies..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. problem with sscanf
    By nevrax in forum C Programming
    Replies: 19
    Last Post: 04-14-2007, 10:59 PM
  3. sscanf problem
    By LordShme in forum C Programming
    Replies: 5
    Last Post: 12-05-2006, 09:09 PM
  4. Weird problem with sscanf
    By g4j31a5 in forum C++ Programming
    Replies: 17
    Last Post: 10-04-2006, 09:16 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM