sscanf Question

Discussion in 'C Programming' started by alij, Jun 2, 2007.

  1. alij

    alij Guest

    Hi,

    I have a string (pointed by variable sourceStr) in the following
    possible formats:

    ? ls : ps
    ? ls -a : touch nfile

    >From the lines above, I want to extract the commands, which are ls, ps

    and ls -a, touch nfile.

    I used sscanf(sourceStr, "? %s : %s", cmd1, cmd2) to extract the
    values, but the problem I'm getting is, it works fine for the first
    example, but not the second example.

    Any Idea if its possible to do this using sscanf function, or on what
    I'm doing wrong? I'll appreciate any pointers.

    Thanks!!

    Alij
     
    alij, Jun 2, 2007
    #1
    1. Advertising

  2. alij <> writes:

    > Hi,
    >
    > I have a string (pointed by variable sourceStr) in the following
    > possible formats:
    >
    > ? ls : ps
    > ? ls -a : touch nfile
    >
    >>From the lines above, I want to extract the commands, which are ls, ps

    > and ls -a, touch nfile.
    >
    > I used sscanf(sourceStr, "? %s : %s", cmd1, cmd2) to extract the
    > values, but the problem I'm getting is, it works fine for the first
    > example, but not the second example.


    Suddenly the scanf family is everybody's favourite.

    You'd be better off using strchr:

    char *divider;
    if ((divider = strchr(sourceStr, ':')) != NULL &&
    divider >= sourceStr + 2) {
    /* the first string runs from sourceStr + 2 and has length
    divider - sourceStr - 2. The second one runs from
    divider (or divider + 1 if the space is *always* there)
    to the end of the string. */
    }

    Obviously more or less checking could be done, depending on what you
    know about the format (more is almost always to be preferred!).

    --
    Ben.
     
    Ben Bacarisse, Jun 2, 2007
    #2
    1. Advertising

  3. On Sat, 02 Jun 2007 04:03:02 -0700, alij <>
    wrote:

    >Hi,
    >
    >I have a string (pointed by variable sourceStr) in the following
    >possible formats:
    >
    >? ls : ps
    >? ls -a : touch nfile
    >
    >>From the lines above, I want to extract the commands, which are ls, ps

    >and ls -a, touch nfile.
    >
    >I used sscanf(sourceStr, "? %s : %s", cmd1, cmd2) to extract the
    >values, but the problem I'm getting is, it works fine for the first
    >example, but not the second example.
    >
    >Any Idea if its possible to do this using sscanf function, or on what
    >I'm doing wrong? I'll appreciate any pointers.


    You are asking sscanf to convert two items. You should test the
    return value to see if it succeeded.

    In the first case, the return value is 2. In the second, it is 1.
    This would have told you that your format string is not doing what you
    want. You need to figure out what <space colon space> between two
    conversion specifications really means. Then you need to determine
    what to replace it with that does what you really want (which is
    probably something along the lines of skip everything until you find a
    colon, skip the colon, and skip the space which is guaranteed to
    follow the colon).

    As Ben suggested, you may be better off not even using sscanf.

    By the way, since a command name can include a path, I hope you made
    cmd1 and cmd2 large enough to accommodate the longest path allowed in
    your system.


    Remove del for email
     
    Barry Schwarz, Jun 2, 2007
    #3
  4. On Sat, 02 Jun 2007 15:44:59 +0100, Ben Bacarisse
    <> wrote:

    >alij <> writes:
    >
    >> Hi,
    >>
    >> I have a string (pointed by variable sourceStr) in the following
    >> possible formats:
    >>
    >> ? ls : ps
    >> ? ls -a : touch nfile
    >>
    >>>From the lines above, I want to extract the commands, which are ls, ps

    >> and ls -a, touch nfile.
    >>
    >> I used sscanf(sourceStr, "? %s : %s", cmd1, cmd2) to extract the
    >> values, but the problem I'm getting is, it works fine for the first
    >> example, but not the second example.

    >
    >Suddenly the scanf family is everybody's favourite.
    >
    >You'd be better off using strchr:
    >
    > char *divider;
    > if ((divider = strchr(sourceStr, ':')) != NULL &&


    Agree with your concept. If the colon can appear other than as a
    command separator (e.g., path name, command argument, etc), it might
    be better to use strstr with a target of " : ".

    > divider >= sourceStr + 2) {
    > /* the first string runs from sourceStr + 2 and has length
    > divider - sourceStr - 2. The second one runs from
    > divider (or divider + 1 if the space is *always* there)
    > to the end of the string. */
    > }
    >
    >Obviously more or less checking could be done, depending on what you
    >know about the format (more is almost always to be preferred!).



    Remove del for email
     
    Barry Schwarz, Jun 2, 2007
    #4
  5. Barry Schwarz <> writes:

    > On Sat, 02 Jun 2007 15:44:59 +0100, Ben Bacarisse
    > <> wrote:
    >
    >>alij <> writes:
    >>
    >>> Hi,
    >>>
    >>> I have a string (pointed by variable sourceStr) in the following
    >>> possible formats:
    >>>
    >>> ? ls : ps
    >>> ? ls -a : touch nfile
    >>>
    >>>>From the lines above, I want to extract the commands, which are ls, ps
    >>> and ls -a, touch nfile.
    >>>
    >>> I used sscanf(sourceStr, "? %s : %s", cmd1, cmd2) to extract the
    >>> values, but the problem I'm getting is, it works fine for the first
    >>> example, but not the second example.

    >>
    >>Suddenly the scanf family is everybody's favourite.
    >>
    >>You'd be better off using strchr:
    >>
    >> char *divider;
    >> if ((divider = strchr(sourceStr, ':')) != NULL &&

    >
    > Agree with your concept. If the colon can appear other than as a
    > command separator (e.g., path name, command argument, etc), it might
    > be better to use strstr with a target of " : ".


    Very neat, yes.

    Following on (from that idea) but directed more to the OP, I have, on
    occasion, found it useful to have null-safe version of the search
    functions, together with one for adding:

    char *ns_add(char *str, ptrdiff_t n)
    {
    return str ? str + n : str;
    }

    char *ns_strstr(char *str, const char *s)
    {
    return str ? strstr(str, s) : str;
    }

    so one can write:

    const char *second = ns_add(ns_strstr(cmd, " : "), 3);
    if (second) ...

    and be sure that the NULL will propagate up even if cmd was NULL.

    --
    Ben.
     
    Ben Bacarisse, Jun 2, 2007
    #5
  6. alij

    alij Guest

    On Jun 2, 12:41 pm, Ben Bacarisse <> wrote:
    > Barry Schwarz <> writes:
    > > On Sat, 02 Jun 2007 15:44:59 +0100, Ben Bacarisse
    > > <> wrote:

    >
    > >>alij <> writes:

    >
    > >>> Hi,

    >
    > >>> I have a string (pointed by variable sourceStr) in the following
    > >>> possible formats:

    >
    > >>> ? ls : ps
    > >>> ? ls -a : touch nfile

    >
    > >>>>From the lines above, I want to extract the commands, which are ls, ps
    > >>> and ls -a, touch nfile.

    >
    > >>> I used sscanf(sourceStr, "? %s : %s", cmd1, cmd2) to extract the
    > >>> values, but the problem I'm getting is, it works fine for the first
    > >>> example, but not the second example.

    >
    > >>Suddenly the scanf family is everybody's favourite.

    >
    > >>You'd be better off using strchr:

    >
    > >> char *divider;
    > >> if ((divider = strchr(sourceStr, ':')) != NULL &&

    >
    > > Agree with your concept. If the colon can appear other than as a
    > > command separator (e.g., path name, command argument, etc), it might
    > > be better to use strstr with a target of " : ".

    >
    > Very neat, yes.
    >
    > Following on (from that idea) but directed more to the OP, I have, on
    > occasion, found it useful to have null-safe version of the search
    > functions, together with one for adding:
    >
    > char *ns_add(char *str, ptrdiff_t n)
    > {
    > return str ? str + n : str;
    >
    > }
    >
    > char *ns_strstr(char *str, const char *s)
    > {
    > return str ? strstr(str, s) : str;
    >
    > }
    >
    > so one can write:
    >
    > const char *second = ns_add(ns_strstr(cmd, " : "), 3);
    > if (second) ...
    >
    > and be sure that the NULL will propagate up even if cmd was NULL.
    >
    > --
    > Ben.- Hide quoted text -
    >
    > - Show quoted text -


    Thank you gentlemen for your help!! I think I will use strstr function
    (as adviced)
     
    alij, Jun 2, 2007
    #6
  7. alij

    alij Guest

    On Jun 2, 4:31 pm, alij <> wrote:
    > On Jun 2, 12:41 pm, Ben Bacarisse <> wrote:
    >
    >
    >
    >
    >
    > > Barry Schwarz <> writes:
    > > > On Sat, 02 Jun 2007 15:44:59 +0100, Ben Bacarisse
    > > > <> wrote:

    >
    > > >>alij <> writes:

    >
    > > >>> Hi,

    >
    > > >>> I have a string (pointed by variable sourceStr) in the following
    > > >>> possible formats:

    >
    > > >>> ? ls : ps
    > > >>> ? ls -a : touch nfile

    >
    > > >>>>From the lines above, I want to extract the commands, which are ls, ps
    > > >>> and ls -a, touch nfile.

    >
    > > >>> I used sscanf(sourceStr, "? %s : %s", cmd1, cmd2) to extract the
    > > >>> values, but the problem I'm getting is, it works fine for the first
    > > >>> example, but not the second example.

    >
    > > >>Suddenly the scanf family is everybody's favourite.

    >
    > > >>You'd be better off using strchr:

    >
    > > >> char *divider;
    > > >> if ((divider = strchr(sourceStr, ':')) != NULL &&

    >
    > > > Agree with your concept. If the colon can appear other than as a
    > > > command separator (e.g., path name, command argument, etc), it might
    > > > be better to use strstr with a target of " : ".

    >
    > > Very neat, yes.

    >
    > > Following on (from that idea) but directed more to the OP, I have, on
    > > occasion, found it useful to have null-safe version of the search
    > > functions, together with one for adding:

    >
    > > char *ns_add(char *str, ptrdiff_t n)
    > > {
    > > return str ? str + n : str;

    >
    > > }

    >
    > > char *ns_strstr(char *str, const char *s)
    > > {
    > > return str ? strstr(str, s) : str;

    >
    > > }

    >
    > > so one can write:

    >
    > > const char *second = ns_add(ns_strstr(cmd, " : "), 3);
    > > if (second) ...

    >
    > > and be sure that the NULL will propagate up even if cmd was NULL.

    >
    > > --
    > > Ben.- Hide quoted text -

    >
    > > - Show quoted text -

    >
    > Thank you gentlemen for your help!! I think I will use strstr function
    > (as adviced)- Hide quoted text -
    >
    > - Show quoted text -


    Sorry, I meant strchr -- thank you once again
     
    alij, Jun 2, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan Smith

    Question on sscanf

    Dan Smith, Aug 14, 2003, in forum: C Programming
    Replies:
    3
    Views:
    414
    Martin Ambuhl
    Aug 14, 2003
  2. Brent Lievers

    sscanf fixed-width integer question

    Brent Lievers, Apr 22, 2004, in forum: C Programming
    Replies:
    8
    Views:
    649
    Dan Pop
    Apr 26, 2004
  3. sscanf() question?

    , Jun 9, 2005, in forum: C Programming
    Replies:
    5
    Views:
    521
  4. broeisi

    Programmer wannabee question about sscanf

    broeisi, Mar 4, 2006, in forum: C Programming
    Replies:
    10
    Views:
    538
    CBFalconer
    Mar 5, 2006
  5. sscanf question

    , Nov 22, 2006, in forum: C Programming
    Replies:
    5
    Views:
    637
Loading...

Share This Page