Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #111489
| From | DFS <nospam@dfs.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Parsing based on 1st and 2nd spaces in a string |
| Date | 2017-06-04 16:43 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <oh1r2f$bml$2@dont-email.me> (permalink) |
| References | <oh0ro2$12p$1@dont-email.me> <g8j8jc93t4tsl1to40hr6uu36bgmak52bk@4ax.com> |
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.
Back to comp.lang.c | Previous | Next — Previous 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