Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #111534
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Parsing based on 1st and 2nd spaces in a string |
| Date | 2017-06-05 12:37 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <87bmq2mzqq.fsf@bsb.me.uk> (permalink) |
| References | <oh0ro2$12p$1@dont-email.me> <m2tw3vamfr.fsf@despina.home> <oh1582$vqs$2@dont-email.me> <8737bfo2j8.fsf@bsb.me.uk> <oh2l63$f7u$1@dont-email.me> |
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.
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