parsing a string

Discussion in 'C Programming' started by AlexB, Mar 18, 2005.

  1. AlexB

    AlexB Guest

    I've been trying to figure out a way to parse the following:

    192.168.0.7;username;password;database

    I'm stumped as to how to do that. Should I look for the first occurance
    of the ';', cut out position x through the position of the delimiter by 1,
    move that string to another string and repeat?

    -Alex
    AlexB, Mar 18, 2005
    #1
    1. Advertising

  2. AlexB wrote:
    > I've been trying to figure out a way to parse the following:
    >
    > 192.168.0.7;username;password;database
    >
    > I'm stumped as to how to do that.


    Parsing IP addresses has been done to death. Search the archives
    of clc.

    > Should I look for the first occurance of the ';', cut out
    > position x through the position of the delimiter by 1,
    > move that string to another string and repeat?


    int ip[4];
    char username[256];
    char password[256];
    char database[256];
    char ignore;

    int r = sscanf(the_string,
    "%3d.%3d.%3d.%3d;%255[^;];%255[^;];%255[^;]%c",
    &ip[0], &ip[1], &ip[2], &ip[3],
    &username, &password, &database, &ignore);

    if (r == 7)
    {
    printf("%3d.%3d.%3d.%3d;%255[^;];%255[^;];%255[^;]%c",
    ip[0], ip[1], ip[2], ip[3],
    username, password, database);
    }

    --
    Peter
    Peter Nilsson, Mar 18, 2005
    #2
    1. Advertising

  3. AlexB

    -berlin.de Guest

    AlexB <> wrote:
    > I've been trying to figure out a way to parse the following:


    > 192.168.0.7;username;password;database


    > I'm stumped as to how to do that. Should I look for the first occurance
    > of the ';', cut out position x through the position of the delimiter by 1,
    > move that string to another string and repeat?


    What about something like the following? Depends of course a bit on
    what you exactly mean by "parsing" etc...

    #include <stdio.h>

    int main( void )
    {
    const char *sl = "192.168.0.7;username;password;database";
    char ip[ 16 ];
    char un[ 100 ];
    char pw[ 100 ];
    char db[ 100 ];

    sscanf( sl, "%15[^;];%99[^;];%99[^;];%99s", ip, un, pw, db );
    printf( "%s - %s - %s - %s\n", ip, un, pw, db );
    return 0;
    }

    And, of course, there's the strtok() function, but you need to know
    what you are doing if you use it.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Mar 18, 2005
    #3
  4. AlexB

    AlexB Guest

    On Thu, 17 Mar 2005 18:01:50 -0800, Peter Nilsson wrote:

    > AlexB wrote:
    >> I've been trying to figure out a way to parse the following:
    >>
    >> 192.168.0.7;username;password;database
    >>
    >> I'm stumped as to how to do that.

    >
    >


    Jens & Peter, thanks! Never thought of that suggestion.. I started going
    with strncpy source,dest,length and then parsing it like that..but yours
    sounds alot better.

    Thanks guys!!

    -Alex
    AlexB, Mar 18, 2005
    #4
  5. Peter Nilsson wrote:
    > printf("%3d.%3d.%3d.%3d;%255[^;];%255[^;];%255[^;]%c",
    > ip[0], ip[1], ip[2], ip[3],
    > username, password, database);


    Make that...

    "%d.%d.%d.%d;%s;%s;%s"

    --
    Peter
    Peter Nilsson, Mar 18, 2005
    #5
  6. AlexB

    Tor Rustad Guest

    Re: parsing a string [OT]

    "AlexB" <> wrote in message
    > I've been trying to figure out a way to parse the following:
    >
    > 192.168.0.7;username;password;database
    >
    > I'm stumped as to how to do that. Should I look for the first

    occurance
    > of the ';', cut out position x through the position of the delimiter

    by 1,
    > move that string to another string and repeat?


    The proposed sscanf() solutions isn't very robust.

    1. Do you only need to consider IPv4 addresses (what about IPv6)?
    2. isn't ';' a legal character in passwords? If so, why not???
    3. consider using a more robust TLV scheme, instead of this comma
    separated list.


    --
    Tor <torust AT online DOT no>
    Tor Rustad, Mar 18, 2005
    #6
  7. AlexB

    AlexB Guest

    Re: parsing a string [OT]

    On Fri, 18 Mar 2005 14:38:11 +0100, Tor Rustad wrote:

    > "AlexB" <> wrote in message
    >> I've been trying to figure out a way to parse the following:
    >>
    >> 192.168.0.7;username;password;database
    >>
    >> I'm stumped as to how to do that. Should I look for the first

    > occurance
    >> of the ';', cut out position x through the position of the delimiter

    > by 1,
    >> move that string to another string and repeat?

    >
    > The proposed sscanf() solutions isn't very robust.
    >
    > 1. Do you only need to consider IPv4 addresses (what about IPv6)?
    > 2. isn't ';' a legal character in passwords? If so, why not???
    > 3. consider using a more robust TLV scheme, instead of this comma
    > separated list.


    yes just IPv4.. its basically for me.. I'm writing a little piece of
    software that connects to a MySQL db and stores info in it.
    You know..I tried the following and it didnt work..

    instead of a fgets I used:
    fscanf( temp, "%15[^;];%99[^;];%99[^;];%99s", &hostname, &username,
    &password, &database);

    all variables were parsed except the hostname..
    so I did a fgets into temp and then
    sscanf( temp, "%15[^;];%99[^;];%99[^;];%99s", &hostname, &username,
    &password, &database);

    and it worked.. weird.

    -Alex
    AlexB, Mar 19, 2005
    #7
  8. AlexB

    AlexB Guest

    Re: parsing a string [OT]

    On Sat, 19 Mar 2005 16:36:12 +0000, AlexB wrote:

    > instead of a fgets I used:
    > fscanf( temp, "%15[^;];%99[^;];%99[^;];%99s", &hostname, &username,
    > &password, &database);


    little typo.. where temp is, I have fp.
    AlexB, Mar 19, 2005
    #8
    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. GIMME
    Replies:
    2
    Views:
    873
    GIMME
    Feb 11, 2004
  2. Naren
    Replies:
    0
    Views:
    579
    Naren
    May 11, 2004
  3. Christopher Diggins
    Replies:
    0
    Views:
    608
    Christopher Diggins
    Jul 9, 2007
  4. Christopher Diggins
    Replies:
    0
    Views:
    432
    Christopher Diggins
    Jul 9, 2007
  5. John Levine
    Replies:
    0
    Views:
    729
    John Levine
    Feb 2, 2012
Loading...

Share This Page