Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > parsing a string

Reply
Thread Tools

parsing a string

 
 
AlexB
Guest
Posts: n/a
 
      03-18-2005
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
 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      03-18-2005
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

 
Reply With Quote
 
 
 
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      03-18-2005
AlexB <(E-Mail Removed)> 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 ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
AlexB
Guest
Posts: n/a
 
      03-18-2005
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
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      03-18-2005
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

 
Reply With Quote
 
Tor Rustad
Guest
Posts: n/a
 
      03-18-2005
"AlexB" <(E-Mail Removed)> 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>

 
Reply With Quote
 
AlexB
Guest
Posts: n/a
 
      03-19-2005
On Fri, 18 Mar 2005 14:38:11 +0100, Tor Rustad wrote:

> "AlexB" <(E-Mail Removed)> 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

 
Reply With Quote
 
AlexB
Guest
Posts: n/a
 
      03-19-2005
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What libraries should I use for MIME parsing, XML parsing, and MySQL ? John Levine Ruby 0 02-02-2012 11:15 PM
[ANN] Parsing Tutorial and YARD 1.0: A C++ Parsing Framework Christopher Diggins C++ 0 07-09-2007 09:01 PM
[ANN] Parsing Tutorial and YARD 1.0: A C++ Parsing Framework Christopher Diggins C++ 0 07-09-2007 08:58 PM
SAX Parsing - Weird results when parsing content between tags. Naren XML 0 05-11-2004 07:25 PM
Perl expression for parsing CSV (ignoring parsing commas when in double quotes) GIMME Perl 2 02-11-2004 05:40 PM



Advertisments