Question on sscanf

Discussion in 'C Programming' started by Dan Smith, Aug 14, 2003.

  1. Dan Smith

    Dan Smith Guest

    Ok, I'm stumped. Either sscanf is broken or I'm not understanding
    somthing. Probably the later. Hopefully someone here can help me out
    with this before I chew my own leg off.

    First what I'm trying to do:

    I have a line in a text file that looks like

    'TYPEBE','P6','FFGHT'

    and I'm trying to get the data in the between the first two quote marks
    into to strings for use later in the program.


    The code I'm using is:

    char line[600];
    char string1[7];
    char string2[7];

    .....

    fgets (line,256,xiresults_file);
    sscanf (line,"%*c%[^']s%*c%*c%*c%[^']s",string1,string2);

    .....

    This code sets string1 to TYPEBE, but string 2 is not being set to P6
    as I expected it to. I'm probably doing something wrong, but I
    just can't figure out what. Thanks in advance for the help

    --
    Dan Smith

    "You don't have to be crazy to work here, we have on the job training"
     
    Dan Smith, Aug 14, 2003
    #1
    1. Advertising

  2. Dan Smith

    Jirka Klaue Guest

    Dan Smith wrote:
    > Ok, I'm stumped. Either sscanf is broken or I'm not understanding
    > somthing. Probably the later. Hopefully someone here can help me out
    > with this before I chew my own leg off.
    >
    > First what I'm trying to do:
    >
    > I have a line in a text file that looks like
    >
    > 'TYPEBE','P6','FFGHT'
    >
    > and I'm trying to get the data in the between the first two quote marks
    > into to strings for use later in the program.
    >
    > The code I'm using is:
    >
    > char line[600];
    > char string1[7];
    > char string2[7];
    >
    > ....
    >
    > fgets (line,256,xiresults_file);
    > sscanf (line,"%*c%[^']s%*c%*c%*c%[^']s",string1,string2);

    ^ ^^
    %[] and %s are distinct conversion specifiers.

    sscanf(s, "%*c%[^']%*c%*c%*c%[^']s", string1, string2);
    or
    sscanf(s, "%*c%[^']%*3s%[^']s", string1, string2);

    Jirka
     
    Jirka Klaue, Aug 14, 2003
    #2
    1. Advertising

  3. Dan Smith

    Jirka Klaue Guest

    I wrote:
    ....
    > sscanf(s, "%*c%[^']%*c%*c%*c%[^']s", string1, string2);

    ^
    > sscanf(s, "%*c%[^']%*3s%[^']s", string1, string2);

    ^
    Forgot do delete these s', too.

    Jirka
     
    Jirka Klaue, Aug 14, 2003
    #3
  4. Dan Smith wrote:
    > Ok, I'm stumped. Either sscanf is broken or I'm not understanding
    > somthing. Probably the later. Hopefully someone here can help me out
    > with this before I chew my own leg off.
    >
    > First what I'm trying to do:
    >
    > I have a line in a text file that looks like
    >
    > 'TYPEBE','P6','FFGHT'
    >
    > and I'm trying to get the data in the between the first two quote marks
    > into to strings for use later in the program.
    >
    >
    > The code I'm using is:
    >
    > char line[600];
    > char string1[7];
    > char string2[7];
    >
    > ....
    >
    > fgets (line,256,xiresults_file);
    > sscanf (line,"%*c%[^']s%*c%*c%*c%[^']s",string1,string2);
    >
    > ....
    >
    > This code sets string1 to TYPEBE, but string 2 is not being set to P6
    > as I expected it to. I'm probably doing something wrong, but I
    > just can't figure out what.


    Mainly, it's that you have extraneous literal 's's in your format string.

    #include <stdio.h>


    int main(void)
    {
    char line[] = "'TYPEBE','P6','FFGHT'";
    char String1[7];
    char String2[7];
    sscanf(line, "'%[^']','%[^']", String1, String2);
    printf("Input line: \"%s\"\n", line);
    printf("Substrings: \"%s\" and \"%s\"\n", String1, String2);
    return 0;
    }


    Input line: "'TYPEBE','P6','FFGHT'"
    Substrings: "TYPEBE" and "P6"


    --
    Martin Ambuhl
     
    Martin Ambuhl, Aug 14, 2003
    #4
    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. Brent Lievers

    sscanf fixed-width integer question

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

    , Jun 9, 2005, in forum: C Programming
    Replies:
    5
    Views:
    525
  3. broeisi

    Programmer wannabee question about sscanf

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

    , Nov 22, 2006, in forum: C Programming
    Replies:
    5
    Views:
    638
  5. alij

    sscanf Question

    alij, Jun 2, 2007, in forum: C Programming
    Replies:
    6
    Views:
    348
Loading...

Share This Page