Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Bartc <bc@freeuk.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Reading beyond the end of file |
| Date | 2015-08-18 00:53 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <mqts2p$js0$1@dont-email.me> (permalink) |
| References | <b62ba036-de31-4f37-aaad-3d1372d40d3f@googlegroups.com> |
On 18/08/2015 00:24, janus wrote:
> Hello All,
>
> I am not getting the expected result from the below code whenever I change '\0' to EOF. I noticed that the below will read pass the end of file. What is it that I am doing wrong? Could someone help me out?
What's the input format? Is it a string terminated with \0, or with EOF?
Note that EOF, normally set to -1, might not be properly representable
when the 'char' type is unsigned (it will be 255). It might work, but I
think 0 is a better terminator if it's going to be a char value.
> static tokens* tokenize(char* source)
> {
> int ch;
> tokens *ntoken;
> char* ctemp;
> //printf("%s\n",source);
> while((ch = *source++) != '\0')
It probably doesn't matter, as ch is checked, but you allow source to
point past the 0/EOF character. I wouldn't advance source past the
terminator, just in case some code then tries to use *source.
Perhaps temporarily use a function to obtain the next character from
source (a bit fiddly, it needs to take a char** parameter so that it can
update the source pointer within the function), then it is easier to
stop it going past the EOF point.
--
Bartc
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Reading beyond the end of file janus <emekamicro@gmail.com> - 2015-08-17 16:24 -0700
Re: Reading beyond the end of file Bartc <bc@freeuk.com> - 2015-08-18 00:53 +0100
Re: Reading beyond the end of file Keith Thompson <kst-u@mib.org> - 2015-08-17 18:44 -0700
Re: Reading beyond the end of file Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-06 14:41 -0700
Re: Reading beyond the end of file Keith Thompson <kst-u@mib.org> - 2015-09-06 17:09 -0700
Re: Reading beyond the end of file James Kuyper <jameskuyper@verizon.net> - 2015-08-17 20:10 -0400
Re: Reading beyond the end of file janus <emekamicro@gmail.com> - 2015-08-18 00:54 -0700
Re: Reading beyond the end of file janus <emekamicro@gmail.com> - 2015-08-18 01:43 -0700
Re: Reading beyond the end of file James Kuyper <jameskuyper@verizon.net> - 2015-08-18 10:18 -0400
Re: Reading beyond the end of file Barry Schwarz <schwarzb@dqel.com> - 2015-08-18 09:18 -0700
Re: Reading beyond the end of file Keith Thompson <kst-u@mib.org> - 2015-08-18 11:17 -0700
Re: Reading beyond the end of file Barry Schwarz <schwarzb@dqel.com> - 2015-08-18 13:24 -0700
csiph-web