Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #111517

Re: Parsing based on 1st and 2nd spaces in a string

From raltbos@xs4all.nl (Richard Bos)
Newsgroups comp.lang.c
Subject Re: Parsing based on 1st and 2nd spaces in a string
Date 2017-06-05 08:52 +0000
Organization Go wash your mouth.
Message-ID <59351a55.1154875@news.xs4all.nl> (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>

Show all headers | View raw


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

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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