scanf question

Discussion in 'C Programming' started by mthies@ww.uni-erlangen.de, Oct 7, 2005.

  1. -erlangen.de

    -erlangen.de Guest

    I have two kinds of file names to read in:
    "name_a01_002"
    "name_a01_opt02_003"
    where the numbers will always have the same amount of digits and will
    thus always be integers. I am only interested in the first and the last
    number and want to set up scanf to give me these values.
    I tried something like
    int a,b
    if( sscanf(filename, "name_a%2d%_*[^_]_%3d", &a, &b) == 2) success=1;
    but it works only for the later one.
    Is it at all possible to skip the middle part (_opt02) optionally?

    Michael
     
    -erlangen.de, Oct 7, 2005
    #1
    1. Advertising

  2. -erlangen.de

    Alex Fraser Guest

    <-erlangen.de> wrote in message
    news:...
    > I have two kinds of file names to read in:
    > "name_a01_002"
    > "name_a01_opt02_003"
    > where the numbers will always have the same amount of digits and will
    > thus always be integers. I am only interested in the first and the last
    > number and want to set up scanf to give me these values.
    > I tried something like
    > int a,b
    > if( sscanf(filename, "name_a%2d%_*[^_]_%3d", &a, &b) == 2) success=1;
    > but it works only for the later one.
    > Is it at all possible to skip the middle part (_opt02) optionally?


    There may be some cunning way to do it, but it would probably be simpler to
    follow the call above with another that matches the other format (if the
    first one did not match).

    Alex
     
    Alex Fraser, Oct 7, 2005
    #2
    1. Advertising

  3. -erlangen.de

    Suman Guest

    -erlangen.de wrote:
    > I have two kinds of file names to read in:
    > "name_a01_002"
    > "name_a01_opt02_003"
    > where the numbers will always have the same amount of digits and will
    > thus always be integers. I am only interested in the first and the last
    > number and want to set up scanf to give me these values.
    > I tried something like
    > int a,b
    > if( sscanf(filename, "name_a%2d%_*[^_]_%3d", &a, &b) == 2) success=1;


    1. What is the '%' bfore the second underscore doing?
    2. The first string (to be parsed) has 2 unbderscores and the second
    three.
    However, you are always trying to match 3 in your scanf call. Is
    that fair?

    > but it works only for the later one.
    > Is it at all possible to skip the middle part (_opt02) optionally?
     
    Suman, Oct 7, 2005
    #3
  4. -erlangen.de

    Guest

    -erlangen.de wrote:
    > I have two kinds of file names to read in:
    > "name_a01_002"
    > "name_a01_opt02_003"
    > where the numbers will always have the same amount of digits and will
    > thus always be integers. I am only interested in the first and the last
    > number and want to set up scanf to give me these values.
    > I tried something like
    > int a,b
    > if( sscanf(filename, "name_a%2d%_*[^_]_%3d", &a, &b) == 2) success=1;
    > but it works only for the later one.
    > Is it at all possible to skip the middle part (_opt02) optionally?


    Not with one scanf. But it can be done in one statement:
    success = (
    (q=p=-1) /* Initialize to sentinels */
    && sscanf(filename,"name_a%2d_%n%*[^_]_%n",&a,&p,&q) /* at
    least */
    && p>0 /* Check for underscore after first number */
    && sscanf(&filename[q<0?p:q],"%3d",&b)
    ));
    You can of course make sure that %2d does read 2 characters and not 1
    etc.

    But, before using something like this, I would ask myself: Is it
    obvious that something like this does not unwittingly invoke undefined
    behaviour and will that be as obvious when I look at it again? Would I
    recognize this construct easily next time I saw it? Is the correctness
    obvious enough that I will know it is not the cause of a bug I start
    debugging next? Will I be able to change it easily? And, will I stay
    alive when the a programmer inherits my code and notices this statement
    ....
     
    , Oct 7, 2005
    #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. merman

    Newbie-question: scanf alternatives?

    merman, Sep 20, 2004, in forum: C Programming
    Replies:
    55
    Views:
    1,355
    Dan Pop
    Sep 24, 2004
  2. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc.

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 16, 2006, in forum: C Programming
    Replies:
    185
    Views:
    3,450
    those who know me have no need of my name
    Apr 3, 2006
  3. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Apr 26, 2006, in forum: C Programming
    Replies:
    18
    Views:
    690
    Richard Bos
    May 2, 2006
  4. john

    scanf question using %[^$]

    john, Aug 22, 2006, in forum: C Programming
    Replies:
    8
    Views:
    332
    Dave Thompson
    Sep 4, 2006
  5. Mik0b0

    a scanf() question

    Mik0b0, Nov 28, 2006, in forum: C Programming
    Replies:
    8
    Views:
    364
    Simon Biber
    Nov 30, 2006
Loading...

Share This Page