Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #111456 > unrolled thread
| Started by | DFS <nospam@dfs.com> |
|---|---|
| First post | 2017-06-04 07:48 -0400 |
| Last post | 2017-06-04 16:43 -0400 |
| Articles | 14 — 8 participants |
Back to article view | Back to comp.lang.c
Parsing based on 1st and 2nd spaces in a string DFS <nospam@dfs.com> - 2017-06-04 07:48 -0400
Re: Parsing based on 1st and 2nd spaces in a string "Pascal J. Bourguignon" <pjb@informatimago.com> - 2017-06-04 15:53 +0200
Re: Parsing based on 1st and 2nd spaces in a string DFS <nospam@dfs.com> - 2017-06-04 10:29 -0400
Re: Parsing based on 1st and 2nd spaces in a string "Pascal J. Bourguignon" <pjb@informatimago.com> - 2017-06-04 21:52 +0200
Re: Parsing based on 1st and 2nd spaces in a string DFS <nospam@dfs.com> - 2017-06-04 16:43 -0400
Re: Parsing based on 1st and 2nd spaces in a string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-04 22:39 +0100
Re: Parsing based on 1st and 2nd spaces in a string Keith Thompson <kst-u@mib.org> - 2017-06-04 19:40 -0700
Re: Parsing based on 1st and 2nd spaces in a string DFS <nospam@dfs.com> - 2017-06-05 00:08 -0400
Re: Parsing based on 1st and 2nd spaces in a string raltbos@xs4all.nl (Richard Bos) - 2017-06-05 08:52 +0000
Re: Parsing based on 1st and 2nd spaces in a string Robert Wessel <robertwessel2@yahoo.com> - 2017-06-05 23:51 -0500
Re: Parsing based on 1st and 2nd spaces in a string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-05 12:37 +0100
Re: Parsing based on 1st and 2nd spaces in a string "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-05 12:53 -0400
Re: Parsing based on 1st and 2nd spaces in a string Barry Schwarz <schwarzb@dqel.com> - 2017-06-04 11:24 -0700
Re: Parsing based on 1st and 2nd spaces in a string DFS <nospam@dfs.com> - 2017-06-04 16:43 -0400
| From | DFS <nospam@dfs.com> |
|---|---|
| Date | 2017-06-04 07:48 -0400 |
| Subject | Parsing based on 1st and 2nd spaces in a string |
| Message-ID | <oh0ro2$12p$1@dont-email.me> |
I have text input in only two formats:
Value line goes like this...
# Value line goes like this...
Value is of varying lengths, but never > 20.
# is always numeric, and never exceeds 9.
Goal is to capture Value and '# Value'
I'm currently using a combination of strchr() and isdigit(), which works
fine, but I'm wondering if there's a better way to handle the lines that
begin with '# Value'.
current code is:
-----------------------------------------------------------
if (!isdigit(line[0]))
{ // no leading digit so parse on 1st space
strncpy(thevalue, line, strchr(line, ' ') - line);
}
else
index=(strchr(line,' ')-line)+1; //get position of 1st space
for(i=index;i<30;i++) //find and parse on 2nd space
{
if(line[i] == ' ')
{ strncpy(thevalue, line, i);
break;
}
}
-----------------------------------------------------------
Ideas or suggestions?
Thanks!
[toc] | [next] | [standalone]
| From | "Pascal J. Bourguignon" <pjb@informatimago.com> |
|---|---|
| Date | 2017-06-04 15:53 +0200 |
| Message-ID | <m2tw3vamfr.fsf@despina.home> |
| In reply to | #111456 |
DFS <nospam@dfs.com> writes:
> I have text input in only two formats:
>
> Value line goes like this...
> # Value line goes like this...
>
> Value is of varying lengths, but never > 20.
> # is always numeric, and never exceeds 9.
>
> Goal is to capture Value and '# Value'
>
> I'm currently using a combination of strchr() and isdigit(), which
> works fine, but I'm wondering if there's a better way to handle the
> lines that begin with '# Value'.
>
> current code is:
> -----------------------------------------------------------
> if (!isdigit(line[0]))
> { // no leading digit so parse on 1st space
> strncpy(thevalue, line, strchr(line, ' ') - line);
> }
> else
> index=(strchr(line,' ')-line)+1; //get position of 1st space
> for(i=index;i<30;i++) //find and parse on 2nd space
> {
> if(line[i] == ' ')
> { strncpy(thevalue, line, i);
> break;
> }
> }
> -----------------------------------------------------------
>
> Ideas or suggestions?
You have a third case that you didn't specify:
#Value
if(isdigit(line[0]) && isspace(line[1])){
parse_digit_and_value(line);
}else if(isdigit(line[0])){
parse_error(line,"Invalid syntax");
}else{
parse_value(line);
}
--
__Pascal J. Bourguignon
http://www.informatimago.com
[toc] | [prev] | [next] | [standalone]
| From | DFS <nospam@dfs.com> |
|---|---|
| Date | 2017-06-04 10:29 -0400 |
| Message-ID | <oh1582$vqs$2@dont-email.me> |
| In reply to | #111466 |
On 6/4/2017 9:53 AM, Pascal J. Bourguignon wrote:
> You have a third case that you didn't specify:
?
As the first line of my post said:
"I have text input in only two formats:"
There is no third case.
But thanks for showing isspace(). That might be handy.
> #Value
>
>
> if(isdigit(line[0]) && isspace(line[1])){
> parse_digit_and_value(line);
> }else if(isdigit(line[0])){
> parse_error(line,"Invalid syntax");
> }else{
> parse_value(line);
> }
[toc] | [prev] | [next] | [standalone]
| From | "Pascal J. Bourguignon" <pjb@informatimago.com> |
|---|---|
| Date | 2017-06-04 21:52 +0200 |
| Message-ID | <m2inkba5u1.fsf@despina.home> |
| In reply to | #111468 |
DFS <nospam@dfs.com> writes: > On 6/4/2017 9:53 AM, Pascal J. Bourguignon wrote: > > >> You have a third case that you didn't specify: > > ? > > As the first line of my post said: > "I have text input in only two formats:" > > There is no third case. Until an attacker feeds your program with the third case, and empties your bank accounts, or sends a missile over your house. -- __Pascal J. Bourguignon http://www.informatimago.com
[toc] | [prev] | [next] | [standalone]
| From | DFS <nospam@dfs.com> |
|---|---|
| Date | 2017-06-04 16:43 -0400 |
| Message-ID | <oh1r1p$bml$1@dont-email.me> |
| In reply to | #111485 |
On 6/4/2017 3:52 PM, Pascal J. Bourguignon wrote: > DFS <nospam@dfs.com> writes: > >> On 6/4/2017 9:53 AM, Pascal J. Bourguignon wrote: >> >> >>> You have a third case that you didn't specify: >> >> ? >> >> As the first line of my post said: >> "I have text input in only two formats:" >> >> There is no third case. > > Until an attacker feeds your program with the third case, and empties > your bank accounts, or sends a missile over your house. Now I'm really scared...
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2017-06-04 22:39 +0100 |
| Message-ID | <8737bfo2j8.fsf@bsb.me.uk> |
| In reply to | #111468 |
DFS <nospam@dfs.com> writes:
> On 6/4/2017 9:53 AM, Pascal J. Bourguignon wrote:
>
>
>> You have a third case that you didn't specify:
>
> ?
>
> As the first line of my post said:
> "I have text input in only two formats:"
>
> There is no third case.
The format is not exactly clear. If it is as you show then there is no
need to search for the space when seeing a digit -- you know exactly
where it it. The fact that you do search for it suggests that you
expect some flexibility or unusual input.
Also, when looking for a second space, you parse nothing at all if a
space is not found into the area (withing 30 characters). In that sense
you already parse three different formats.
Another point: are you absolutely sure that strncpy is the function you
need? Unlike the other string functions it does append a null byte so
you can end up with unexpected result. And if it is the right function
to use, the modern way is to call memcpy. It's clearer in the the
reader has fewer things to think about -- it's a straight data copy.
> But thanks for showing isspace(). That might be handy.
>
>> if(isdigit(line[0]) && isspace(line[1])){
>> parse_digit_and_value(line);
>> }else if(isdigit(line[0])){
>> parse_error(line,"Invalid syntax");
>> }else{
>> parse_value(line);
>> }
You really should consider making the code as clear as this. There is
almost not cost to covering all the options and lot to be gained by not
causing your readers to wonder about the other case.
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2017-06-04 19:40 -0700 |
| Message-ID | <lnk24rkvgv.fsf@kst-u.example.com> |
| In reply to | #111493 |
Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> DFS <nospam@dfs.com> writes:
[...]
> Another point: are you absolutely sure that strncpy is the function you
> need? Unlike the other string functions it does
You mean "doesn't".
> append a null byte so
> you can end up with unexpected result. And if it is the right function
> to use, the modern way is to call memcpy. It's clearer in the the
> reader has fewer things to think about -- it's a straight data copy.
>
>> But thanks for showing isspace(). That might be handy.
>>
>>> if(isdigit(line[0]) && isspace(line[1])){
>>> parse_digit_and_value(line);
>>> }else if(isdigit(line[0])){
>>> parse_error(line,"Invalid syntax");
>>> }else{
>>> parse_value(line);
>>> }
>
> You really should consider making the code as clear as this. There is
> almost not cost to covering all the options and lot to be gained by not
> causing your readers to wonder about the other case.
Keep in mind that isdigit() takes an argument of type int, and has
undefined behavior for a negative argument other than EOF. Usually
isdigit(foo) should be isdigit((unsigned)foo).
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | DFS <nospam@dfs.com> |
|---|---|
| Date | 2017-06-05 00:08 -0400 |
| Message-ID | <oh2l63$f7u$1@dont-email.me> |
| In reply to | #111493 |
On 6/4/2017 5:39 PM, Ben Bacarisse wrote:
> DFS <nospam@dfs.com> writes:
>
>> On 6/4/2017 9:53 AM, Pascal J. Bourguignon wrote:
>>
>>
>>> You have a third case that you didn't specify:
>>
>> ?
>>
>> As the first line of my post said:
>> "I have text input in only two formats:"
>>
>> There is no third case.
>
> The format is not exactly clear.
First thing I said was:
<quote>
I have text input in only two formats:
Value line goes like this...
# Value line goes like this...
</quote>
What's not clear? Is it because I didn't say Value was always
alphabetic? If so, my bad.
> If it is as you show then there is no
> need to search for the space when seeing a digit -- you know exactly
> where it it.
If line begins with a digit I need to find the /second/ space, starting
at index 2.
> The fact that you do search for it suggests that you
> expect some flexibility or unusual input.
It suggested an embarrassing brainfart on my part. :)
> Also, when looking for a second space, you parse nothing at all if a
> space is not found into the area (withing 30 characters). In that sense
> you already parse three different formats.
I know /for certain/ a space will be found within 30 characters.
> Another point: are you absolutely sure that strncpy is the function you
> need?
No. I just figured I should use a string copy function to copy a substring.
> Unlike the other string functions it does append a null byte so
> you can end up with unexpected result. And if it is the right function
> to use, the modern way is to call memcpy. It's clearer in the the
> reader has fewer things to think about -- it's a straight data copy.
Right now I have:
memset(thevalue,'\0',sizeof(thevalue));
strncpy(thevalue, line, nbr of bytes);
Using memcpy:
memset(thevalue,'\0',sizeof(thevalue));
memcpy(thevalue, line, nbr of bytes);
Tested and the results match.
This site
http://computer-programming-forum.com/47-c-language/e599c264d43e95af.htm
says (in 2001):
"If you have a string (terminated, as all strings are in C, by \0), and
you want to copy at most n characters of it, use strncpy. If you have a
block of exactly n bytes to copy, bytes of arbitrary value which might
or might not contain 0's, use memcpy."
Sounds like he would recommend strncpy for my situation. What do you
think are the advantages/disadvantages of memcpy for this routine?
>> But thanks for showing isspace(). That might be handy.
>>
>>> if(isdigit(line[0]) && isspace(line[1])){
>>> parse_digit_and_value(line);
>>> }else if(isdigit(line[0])){
>>> parse_error(line,"Invalid syntax");
>>> }else{
>>> parse_value(line);
>>> }
>
> You really should consider making the code as clear as this. There is
> almost not cost to covering all the options and lot to be gained by not
> causing your readers to wonder about the other case.
My code is now more clear than that. You guys are fixated on a 3rd
'option' (#Value) that will never occur. Showing it in the code would
confuse readers. Why didn't you guys also bring up imaginary options 4
and 5?
1 Value valid option
2 # Value valid option
3 #Value will never happen
4 Value# will never happen
5 Value # will never happen
With my original, and Barry S's suggestions, I ended up with:
if(!isdigit(line[0]))
{ strncpy(thevalue,line,strchr(line, ' ')-line); }
else
{ strncpy(thevalue,line,strcspn(line+2," ")+2); }
which works fine.
FYI: about 80% of the lines begin with a Value (which is a person's name).
Thanks for your review, Ben.
[toc] | [prev] | [next] | [standalone]
| From | raltbos@xs4all.nl (Richard Bos) |
|---|---|
| Date | 2017-06-05 08:52 +0000 |
| Message-ID | <59351a55.1154875@news.xs4all.nl> |
| In reply to | #111510 |
DFS <nospam@dfs.com> wrote: > On 6/4/2017 5:39 PM, Ben Bacarisse wrote: > > The format is not exactly clear. > > First thing I said was: > > <quote> > I have text input in only two formats: > > Value line goes like this... > # Value line goes like this... > </quote> > > What's not clear? Those formats. > Is it because I didn't say Value was always alphabetic? That's the most important one, yes. But what Pascal was trying, in his usual graceful way, to say is: what are you going to do if you encounter a line that _doesn't_ follow either of your formats? Don't say it can't happen. You have a file. Wannacry may have happened to it. > > Also, when looking for a second space, you parse nothing at all if a > > space is not found into the area (withing 30 characters). In that sense > > you already parse three different formats. > > I know /for certain/ a space will be found within 30 characters. No, you know for certain that it will be found _if your file is correct_. If you want to program out there in the real world, you'll have to learn to assume that it may not be. The real world is unreliable, and often goes wrong at the most inconvenient of moments. > Right now I have: > memset(thevalue,'\0',sizeof(thevalue)); > strncpy(thevalue, line, nbr of bytes); Replace strncpy() and strncat() and you'll have a more reliable, slightly more efficient program, for exaxtly the reasons Ben mentioned. > This site > http://computer-programming-forum.com/47-c-language/e599c264d43e95af.htm > says (in 2001): > "If you have a string (terminated, as all strings are in C, by \0), and > you want to copy at most n characters of it, use strncpy. If you have a > block of exactly n bytes to copy, bytes of arbitrary value which might > or might not contain 0's, use memcpy." That site it wrong. > Sounds like he would recommend strncpy for my situation. What do you > think are the advantages/disadvantages of memcpy for this routine? memcpy() is not the function to replace strncpy() with. strncat() is. > My code is now more clear than that. You guys are fixated on a 3rd > 'option' (#Value) that will never occur. Famous Last Words. > 1 Value valid option > 2 # Value valid option > 3 #Value will never happen > 4 Value# will never happen > 5 Value # will never happen _Should_ never happen, but inevitably _will_ the moment you demonstrate your perfect code to your superior. Richard
[toc] | [prev] | [next] | [standalone]
| From | Robert Wessel <robertwessel2@yahoo.com> |
|---|---|
| Date | 2017-06-05 23:51 -0500 |
| Message-ID | <n2dcjc52fa5ddhknvgalt3smo3kv38cur4@4ax.com> |
| In reply to | #111517 |
On Mon, 05 Jun 2017 08:52:48 GMT, raltbos@xs4all.nl (Richard Bos) wrote: >DFS <nospam@dfs.com> wrote: > >> On 6/4/2017 5:39 PM, Ben Bacarisse wrote: > >> > The format is not exactly clear. >> >> First thing I said was: >> >> <quote> >> I have text input in only two formats: >> >> Value line goes like this... >> # Value line goes like this... >> </quote> >> >> What's not clear? > >Those formats. > >> Is it because I didn't say Value was always alphabetic? > >That's the most important one, yes. > >But what Pascal was trying, in his usual graceful way, to say is: what >are you going to do if you encounter a line that _doesn't_ follow either >of your formats? Don't say it can't happen. You have a file. Wannacry >may have happened to it. > >> > Also, when looking for a second space, you parse nothing at all if a >> > space is not found into the area (withing 30 characters). In that sense >> > you already parse three different formats. >> >> I know /for certain/ a space will be found within 30 characters. > >No, you know for certain that it will be found _if your file is >correct_. If you want to program out there in the real world, you'll >have to learn to assume that it may not be. The real world is >unreliable, and often goes wrong at the most inconvenient of moments. In 2017, the "real world" must usually be assumed to be actively malicious.
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2017-06-05 12:37 +0100 |
| Message-ID | <87bmq2mzqq.fsf@bsb.me.uk> |
| In reply to | #111510 |
DFS <nospam@dfs.com> writes:
> On 6/4/2017 5:39 PM, Ben Bacarisse wrote:
>> DFS <nospam@dfs.com> writes:
>>
>>> On 6/4/2017 9:53 AM, Pascal J. Bourguignon wrote:
>>>
>>>
>>>> You have a third case that you didn't specify:
>>>
>>> ?
>>>
>>> As the first line of my post said:
>>> "I have text input in only two formats:"
>>>
>>> There is no third case.
>>
>> The format is not exactly clear.
>
> First thing I said was:
>
> <quote>
> I have text input in only two formats:
>
> Value line goes like this...
> # Value line goes like this...
> </quote>
>
> What's not clear? Is it because I didn't say Value was always
> alphabetic? If so, my bad.
No, it was the miss-match between the apparent rigidity of the format
and the flexibility permitted by the code. Given the rigid format the
code could be simpler. Not that I think that's the best approach, min
you -- format have a habit of changing, and data has a habit of getting
corrupted.
I've cut the exchange about the details because I need to check
something else first. You posted this:
if (!isdigit(line[0]))
{ // no leading digit so parse on 1st space
strncpy(thevalue, line, strchr(line, ' ') - line);
}
else
index=(strchr(line,' ')-line)+1; //get position of 1st space
for(i=index;i<30;i++) //find and parse on 2nd space
{
if(line[i] == ' ')
{ strncpy(thevalue, line, i);
break;
}
}
but the indentation is misleading. The for loop is executed even when
there is no initial digit and thevalue has alread been set. I can't
tell what happens of course, because I don't know what value index has
at that point so maybe it all works but it's very confusing.
<snip>
>> Another point: are you absolutely sure that strncpy is the function you
>> need?
>
> No. I just figured I should use a string copy function to copy a
> substring.
>
>> Unlike the other string functions it does append a null byte so
>> you can end up with unexpected result. And if it is the right function
>> to use, the modern way is to call memcpy. It's clearer in the the
>> reader has fewer things to think about -- it's a straight data copy.
>
> Right now I have:
> memset(thevalue,'\0',sizeof(thevalue));
> strncpy(thevalue, line, nbr of bytes);
>
> Using memcpy:
> memset(thevalue,'\0',sizeof(thevalue));
> memcpy(thevalue, line, nbr of bytes);
I'd write
memcpy(thevalue, line, nbr_of_bytes);
theline[nbr_of_bytes] = 0;
Another tip you might find useful is to use strcspn rather than strchr.
It will give you the number you want right away and it's more robust in
the face or errors (I know that's not your concern right now).
But if you are certain of the format, I'd use sscanf:
sscanf(line, "%*[0123456789] %s", thevalue)
will return 1 for the first format. If you don't get 1
sscanf(line, "%s", the value)
will parse the second format. (Always presuming I've understood the
formats.) Given that you know exactly what the input is like, the one
statement:
sscanf(line, "%*[0123456789] %s", thevalue) || sscanf(line, "%s", the value);
is all you need. You don't even need a size inf the %s format because
you are sire the data fits. (And I hate even typing that sentence but I
know I won't persuade you to be more cautious!)
<snip>
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | "James R. Kuyper" <jameskuyper@verizon.net> |
|---|---|
| Date | 2017-06-05 12:53 -0400 |
| Message-ID | <cd7735dd-5ded-878e-da3e-254896e95ff1@verizon.net> |
| In reply to | #111510 |
On 06/05/2017 12:08 AM, DFS wrote: ... >> Another point: are you absolutely sure that strncpy is the function you >> need? > > No. I just figured I should use a string copy function to copy a substring. Which is why you should consider using something other than strncpy(). Despite the "str" in it's name, strncpy() is not exactly a string copy function, for two different reasons. First, if input string is longer than the specified length, what it creates in the output area is not a C string, because it doesn't have a terminating null character. Secondly, if the input string is more than one byte shorter than the specified length, it does more than copy the string, it also nulls-out the remaining positions in the output area. That's fine, if both of those are things that you want to do. However, it's relatively uncommon to need those features of strncpy(). I, personally, have made frequent use of it, but my reasons for doing so are rather uncommon.
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarzb@dqel.com> |
|---|---|
| Date | 2017-06-04 11:24 -0700 |
| Message-ID | <g8j8jc93t4tsl1to40hr6uu36bgmak52bk@4ax.com> |
| In reply to | #111456 |
On Sun, 4 Jun 2017 07:48:31 -0400, DFS <nospam@dfs.com> wrote:
>I have text input in only two formats:
>
>Value line goes like this...
># Value line goes like this...
>
>Value is of varying lengths, but never > 20.
># is always numeric, and never exceeds 9.
>
>Goal is to capture Value and '# Value'
>
>I'm currently using a combination of strchr() and isdigit(), which works
>fine, but I'm wondering if there's a better way to handle the lines that
>begin with '# Value'.
>
>current code is:
>-----------------------------------------------------------
>if (!isdigit(line[0]))
> { // no leading digit so parse on 1st space
> strncpy(thevalue, line, strchr(line, ' ') - line);
> }
>else
> index=(strchr(line,' ')-line)+1; //get position of 1st space
> for(i=index;i<30;i++) //find and parse on 2nd space
> {
> if(line[i] == ' ')
> { strncpy(thevalue, line, i);
> break;
> }
> }
>-----------------------------------------------------------
>
>Ideas or suggestions?
>
>Thanks!
For the second case, since # never exceeds 9, is it not guaranteed
that line[1] == ' ' and index will always be 2? Furthermore, your
code assumes line[2] != ' '. If that is the case, i should never be
greater that 22.
If the above is all true, you can simplify the else to
strncpy(thevalue, line, strcspn(line+2," ")+2);
Presumably you have some other code that insures that thevalue is
properly terminated with a '\0'.
--
Remove del for email
[toc] | [prev] | [next] | [standalone]
| From | DFS <nospam@dfs.com> |
|---|---|
| Date | 2017-06-04 16:43 -0400 |
| Message-ID | <oh1r2f$bml$2@dont-email.me> |
| In reply to | #111482 |
On 6/4/2017 2:24 PM, Barry Schwarz wrote:
> On Sun, 4 Jun 2017 07:48:31 -0400, DFS <nospam@dfs.com> wrote:
>
>> I have text input in only two formats:
>>
>> Value line goes like this...
>> # Value line goes like this...
>>
>> Value is of varying lengths, but never > 20.
>> # is always numeric, and never exceeds 9.
>>
>> Goal is to capture Value and '# Value'
>>
>> I'm currently using a combination of strchr() and isdigit(), which works
>> fine, but I'm wondering if there's a better way to handle the lines that
>> begin with '# Value'.
>>
>> current code is:
>> -----------------------------------------------------------
>> if (!isdigit(line[0]))
>> { // no leading digit so parse on 1st space
>> strncpy(thevalue, line, strchr(line, ' ') - line);
>> }
>> else
>> index=(strchr(line,' ')-line)+1; //get position of 1st space
>> for(i=index;i<30;i++) //find and parse on 2nd space
>> {
>> if(line[i] == ' ')
>> { strncpy(thevalue, line, i);
>> break;
>> }
>> }
>> -----------------------------------------------------------
>>
>> Ideas or suggestions?
>>
>> Thanks!
>
> For the second case, since # never exceeds 9, is it not guaranteed
> that line[1] == ' ' and index will always be 2?
Good spot: yes, if the first character is a digit, index will always be 2.
What was I thinking? Jeez.
> Furthermore, your
> code assumes line[2] != ' '. If that is the case, i should never be
> greater that 22.
Yeah, I just put in 30 for some wiggle room.
> If the above is all true, you can simplify the else to
> strncpy(thevalue, line, strcspn(line+2," ")+2);
Works great.
This is why I posted. Another set of eyes. Chopped 5 unnecessary lines
from the code.
> Presumably you have some other code that insures that thevalue is
> properly terminated with a '\0'.
Each time before thevalue is re/assigned:
memset(thevalue,'\0',sizeof(thevalue));
Thanks Barry.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web