473,394 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

sscanf Question

Hi,

I have a string (pointed by variable sourceStr) in the following
possible formats:

? ls : ps
? ls -a : touch nfile
>From the lines above, I want to extract the commands, which are ls, ps
and ls -a, touch nfile.

I used sscanf(sourceStr, "? %s : %s", cmd1, cmd2) to extract the
values, but the problem I'm getting is, it works fine for the first
example, but not the second example.

Any Idea if its possible to do this using sscanf function, or on what
I'm doing wrong? I'll appreciate any pointers.

Thanks!!

Alij

Jun 2 '07 #1
6 2211
alij <al*************@gmail.comwrites:
Hi,

I have a string (pointed by variable sourceStr) in the following
possible formats:

? ls : ps
? ls -a : touch nfile
>>From the lines above, I want to extract the commands, which are ls, ps
and ls -a, touch nfile.

I used sscanf(sourceStr, "? %s : %s", cmd1, cmd2) to extract the
values, but the problem I'm getting is, it works fine for the first
example, but not the second example.
Suddenly the scanf family is everybody's favourite.

You'd be better off using strchr:

char *divider;
if ((divider = strchr(sourceStr, ':')) != NULL &&
divider >= sourceStr + 2) {
/* the first string runs from sourceStr + 2 and has length
divider - sourceStr - 2. The second one runs from
divider (or divider + 1 if the space is *always* there)
to the end of the string. */
}

Obviously more or less checking could be done, depending on what you
know about the format (more is almost always to be preferred!).

--
Ben.
Jun 2 '07 #2
On Sat, 02 Jun 2007 04:03:02 -0700, alij <al*************@gmail.com>
wrote:
>Hi,

I have a string (pointed by variable sourceStr) in the following
possible formats:

? ls : ps
? ls -a : touch nfile
>>From the lines above, I want to extract the commands, which are ls, ps
and ls -a, touch nfile.

I used sscanf(sourceStr, "? %s : %s", cmd1, cmd2) to extract the
values, but the problem I'm getting is, it works fine for the first
example, but not the second example.

Any Idea if its possible to do this using sscanf function, or on what
I'm doing wrong? I'll appreciate any pointers.
You are asking sscanf to convert two items. You should test the
return value to see if it succeeded.

In the first case, the return value is 2. In the second, it is 1.
This would have told you that your format string is not doing what you
want. You need to figure out what <space colon spacebetween two
conversion specifications really means. Then you need to determine
what to replace it with that does what you really want (which is
probably something along the lines of skip everything until you find a
colon, skip the colon, and skip the space which is guaranteed to
follow the colon).

As Ben suggested, you may be better off not even using sscanf.

By the way, since a command name can include a path, I hope you made
cmd1 and cmd2 large enough to accommodate the longest path allowed in
your system.
Remove del for email
Jun 2 '07 #3
On Sat, 02 Jun 2007 15:44:59 +0100, Ben Bacarisse
<be********@bsb.me.ukwrote:
>alij <al*************@gmail.comwrites:
>Hi,

I have a string (pointed by variable sourceStr) in the following
possible formats:

? ls : ps
? ls -a : touch nfile
>>>From the lines above, I want to extract the commands, which are ls, ps
and ls -a, touch nfile.

I used sscanf(sourceStr, "? %s : %s", cmd1, cmd2) to extract the
values, but the problem I'm getting is, it works fine for the first
example, but not the second example.

Suddenly the scanf family is everybody's favourite.

You'd be better off using strchr:

char *divider;
if ((divider = strchr(sourceStr, ':')) != NULL &&
Agree with your concept. If the colon can appear other than as a
command separator (e.g., path name, command argument, etc), it might
be better to use strstr with a target of " : ".
divider >= sourceStr + 2) {
/* the first string runs from sourceStr + 2 and has length
divider - sourceStr - 2. The second one runs from
divider (or divider + 1 if the space is *always* there)
to the end of the string. */
}

Obviously more or less checking could be done, depending on what you
know about the format (more is almost always to be preferred!).

Remove del for email
Jun 2 '07 #4
Barry Schwarz <sc******@doezl.netwrites:
On Sat, 02 Jun 2007 15:44:59 +0100, Ben Bacarisse
<be********@bsb.me.ukwrote:
>>alij <al*************@gmail.comwrites:
>>Hi,

I have a string (pointed by variable sourceStr) in the following
possible formats:

? ls : ps
? ls -a : touch nfile

From the lines above, I want to extract the commands, which are ls, ps
and ls -a, touch nfile.

I used sscanf(sourceStr, "? %s : %s", cmd1, cmd2) to extract the
values, but the problem I'm getting is, it works fine for the first
example, but not the second example.

Suddenly the scanf family is everybody's favourite.

You'd be better off using strchr:

char *divider;
if ((divider = strchr(sourceStr, ':')) != NULL &&

Agree with your concept. If the colon can appear other than as a
command separator (e.g., path name, command argument, etc), it might
be better to use strstr with a target of " : ".
Very neat, yes.

Following on (from that idea) but directed more to the OP, I have, on
occasion, found it useful to have null-safe version of the search
functions, together with one for adding:

char *ns_add(char *str, ptrdiff_t n)
{
return str ? str + n : str;
}

char *ns_strstr(char *str, const char *s)
{
return str ? strstr(str, s) : str;
}

so one can write:

const char *second = ns_add(ns_strstr(cmd, " : "), 3);
if (second) ...

and be sure that the NULL will propagate up even if cmd was NULL.

--
Ben.
Jun 2 '07 #5
On Jun 2, 12:41 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
Barry Schwarz <schwa...@doezl.netwrites:
On Sat, 02 Jun 2007 15:44:59 +0100, Ben Bacarisse
<ben.use...@bsb.me.ukwrote:
>alij <aliasger.jaf...@gmail.comwrites:
>Hi,
>I have a string (pointed by variable sourceStr) in the following
possible formats:
>? ls : ps
? ls -a : touch nfile
>>>From the lines above, I want to extract the commands, which are ls, ps
and ls -a, touch nfile.
>I used sscanf(sourceStr, "? %s : %s", cmd1, cmd2) to extract the
values, but the problem I'm getting is, it works fine for the first
example, but not the second example.
>Suddenly the scanf family is everybody's favourite.
>You'd be better off using strchr:
char *divider;
if ((divider = strchr(sourceStr, ':')) != NULL &&
Agree with your concept. If the colon can appear other than as a
command separator (e.g., path name, command argument, etc), it might
be better to use strstr with a target of " : ".

Very neat, yes.

Following on (from that idea) but directed more to the OP, I have, on
occasion, found it useful to have null-safe version of the search
functions, together with one for adding:

char *ns_add(char *str, ptrdiff_t n)
{
return str ? str + n : str;

}

char *ns_strstr(char *str, const char *s)
{
return str ? strstr(str, s) : str;

}

so one can write:

const char *second = ns_add(ns_strstr(cmd, " : "), 3);
if (second) ...

and be sure that the NULL will propagate up even if cmd was NULL.

--
Ben.- Hide quoted text -

- Show quoted text -
Thank you gentlemen for your help!! I think I will use strstr function
(as adviced)

Jun 2 '07 #6
On Jun 2, 4:31 pm, alij <aliasger.jaf...@gmail.comwrote:
On Jun 2, 12:41 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:


Barry Schwarz <schwa...@doezl.netwrites:
On Sat, 02 Jun 2007 15:44:59 +0100, Ben Bacarisse
<ben.use...@bsb.me.ukwrote:
>>alij <aliasger.jaf...@gmail.comwrites:
>>Hi,
>>I have a string (pointed by variable sourceStr) in the following
>>possible formats:
>>? ls : ps
>>? ls -a : touch nfile
>>>>From the lines above, I want to extract the commands, which are ls, ps
>>and ls -a, touch nfile.
>>I used sscanf(sourceStr, "? %s : %s", cmd1, cmd2) to extract the
>>values, but the problem I'm getting is, it works fine for the first
>>example, but not the second example.
>>Suddenly the scanf family is everybody's favourite.
>>You'd be better off using strchr:
> char *divider;
> if ((divider = strchr(sourceStr, ':')) != NULL &&
Agree with your concept. If the colon can appear other than as a
command separator (e.g., path name, command argument, etc), it might
be better to use strstr with a target of " : ".
Very neat, yes.
Following on (from that idea) but directed more to the OP, I have, on
occasion, found it useful to have null-safe version of the search
functions, together with one for adding:
char *ns_add(char *str, ptrdiff_t n)
{
return str ? str + n : str;
}
char *ns_strstr(char *str, const char *s)
{
return str ? strstr(str, s) : str;
}
so one can write:
const char *second = ns_add(ns_strstr(cmd, " : "), 3);
if (second) ...
and be sure that the NULL will propagate up even if cmd was NULL.
--
Ben.- Hide quoted text -
- Show quoted text -

Thank you gentlemen for your help!! I think I will use strstr function
(as adviced)- Hide quoted text -

- Show quoted text -
Sorry, I meant strchr -- thank you once again

Jun 2 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dan Smith | last post by:
Ok, I'm stumped. Either sscanf is broken or I'm not understanding somthing. Probably the later. Hopefully someone here can help me out with this before I chew my own leg off. First what I'm...
8
by: Brent Lievers | last post by:
Greetings, I have a question about parsing a fixed-width integer from a string. Lines of data are being read from a file having a very strict column-delimited format. In my example below,...
12
by: Simone Mehta | last post by:
hi All, I am parsing a CSV file. I want to read every row into a char array of reasonable size and then extract strings from it. <snippet> char foo="hello,world,bye,bye,world"; ........
10
by: baumann | last post by:
hi, 1) first test program code #include <stdio.h> int main(void) { char * file = "aaa 23 32 m 2.23 ammasd"; int i2,i3;
5
by: jchludzinski | last post by:
I'm using strtok() to parse thru a line and read different numbers: float value; char *token; token = strtok( line, " " ); .... sscanf( token, "%f", &value ); These results are less...
10
by: broeisi | last post by:
What advantages does sscanf offer over scanf? I had the following code: #include <stdio.h> #include <string.h> int main(void) { int start, finish, values;
3
by: Jerith | last post by:
Hey guys, I am having a problem with stack corruption in my code (it tells me that the stack around the variable 'code' is corrupted). I think I what is happening is that in both instances of the...
24
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be...
16
by: Chad | last post by:
Given the following #include <stdio.h> int main(void) { char line; long arg1, arg2; while(fgets(line, BUFSIZ, stdin) != NULL){
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.