Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #111510
| From | DFS <nospam@dfs.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Parsing based on 1st and 2nd spaces in a string |
| Date | 2017-06-05 00:08 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <oh2l63$f7u$1@dont-email.me> (permalink) |
| References | <oh0ro2$12p$1@dont-email.me> <m2tw3vamfr.fsf@despina.home> <oh1582$vqs$2@dont-email.me> <8737bfo2j8.fsf@bsb.me.uk> |
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.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web