parsing a string

A

AlexB

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
 
P

Peter Nilsson

AlexB said:
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);
}
 
J

Jens.Toerring

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

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
 
A

AlexB

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
 
P

Peter Nilsson

Peter said:
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"
 
T

Tor Rustad

AlexB said:
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.
 
A

AlexB

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
 
A

AlexB

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

little typo.. where temp is, I have fp.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,137
Latest member
NoelAshwor
Top