Simple sscanf parsing problem

Discussion in 'C Programming' started by Timo, Jun 28, 2008.

  1. Timo

    Timo Guest

    I haven't been using ANSI-C for string parsing for some time, so even
    this simple task is problematic:

    I have a string tmp_str, which includes date + time + newline in
    format: "25.6.2008 21:49".

    I try to parse date from this string to variables tmp1, tmp2, tmp3:

    1st attempt:
    sscanf(tmp_str, "%d.%d.%d", &tmp1, &tmp2, &tmp3);

    All variables get value 0.

    2nd attempt
    sscanf(tmp_str, "%d %d %d", &tmp1, &tmp2, &tmp3);

    tmp3 = 25, others 0.

    Any suggestions?
     
    Timo, Jun 28, 2008
    #1
    1. Advertising

  2. Timo

    Guest

    Timo wrote:
    > Any suggestions?


    Post a compilable complete code that exhibits the behaviour.

    The following program outputs "25 6 2008 21 49".

    =========================

    #include <stdio.h>

    int main(void) {
    int tmp1, tmp2, tmp3, tmp4, tmp5;
    const char date[] = "25.6.2008 21:49";

    sscanf(date, "%d.%d.%d %d:%d", &tmp1, &tmp2, &tmp3, &tmp4, &tmp5);
    printf("%d %d %d %d %d\n", tmp1, tmp2, tmp3, tmp4, tmp5);
    return 0;
    }
     
    , Jun 28, 2008
    #2
    1. Advertising

  3. Timo

    Ali Karaali Guest

    On 28 Haziran, 15:31, Timo <> wrote:
    > I haven't been using ANSI-C for string parsing for some time, so even
    > this simple task is problematic:
    >
    > I have a string tmp_str, which includes date + time + newline in
    > format: "25.6.2008 21:49".
    >
    > I try to parse date from this string to variables tmp1, tmp2, tmp3:
    >
    > 1st attempt:
    > sscanf(tmp_str, "%d.%d.%d", &tmp1, &tmp2, &tmp3);
    >
    > All variables get value 0.
    >
    > 2nd attempt
    > sscanf(tmp_str, "%d %d %d", &tmp1, &tmp2, &tmp3);
    >
    > tmp3 = 25, others 0.
    >
    > Any suggestions?


    You can use
    sscanf (str, "%d%*c%d%*c%d %d%*c%d", &a, &b, &c, &d, &e);
     
    Ali Karaali, Jun 28, 2008
    #3
  4. Timo

    Timo Guest

    Thanks for your replies. I found that the reason for this problem was
    because of variable types:

    unsigned short tmp1;
    unsigned char tmp2, tmp3;

    They should have been int's, or sscanf format parameters should have
    been modified.
     
    Timo, Jun 28, 2008
    #4
  5. Timo

    CBFalconer Guest

    Timo wrote:
    >
    > I have a string tmp_str, which includes date + time + newline in
    > format: "25.6.2008 21:49".
    >
    > I try to parse date from this string to variables tmp1, tmp2, tmp3:
    >
    > 1st attempt:
    > sscanf(tmp_str, "%d.%d.%d", &tmp1, &tmp2, &tmp3);
    >
    > All variables get value 0.
    >
    > 2nd attempt
    > sscanf(tmp_str, "%d %d %d", &tmp1, &tmp2, &tmp3);
    >
    > tmp3 = 25, others 0.
    >
    > Any suggestions?


    Never use a scanf function without checking the error return.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
     
    CBFalconer, Jun 28, 2008
    #5
    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. PX

    Weird problem: sscanf

    PX, Feb 18, 2004, in forum: C Programming
    Replies:
    3
    Views:
    506
    Mark McIntyre
    Feb 23, 2004
  2. jacob navia

    sscanf problem

    jacob navia, Jul 9, 2004, in forum: C Programming
    Replies:
    6
    Views:
    348
    jacob navia
    Jul 10, 2004
  3. Simone Mehta

    sscanf parsing doubt

    Simone Mehta, Sep 26, 2004, in forum: C Programming
    Replies:
    12
    Views:
    729
    Aslam Sheikh Durrani
    Oct 3, 2004
  4. Simone Mehta

    sscanf parsing doubt

    Simone Mehta, Sep 27, 2004, in forum: C Programming
    Replies:
    0
    Views:
    319
    Simone Mehta
    Sep 27, 2004
  5. Markus Ilmola

    sscanf style string parsing

    Markus Ilmola, Mar 10, 2006, in forum: C++
    Replies:
    6
    Views:
    1,354
    Default User
    Mar 11, 2006
Loading...

Share This Page