Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: while loop vs do while loop reading from the file issue Date: Sun, 11 Dec 2016 12:56:10 -0800 Organization: None to speak of Lines: 32 Message-ID: References: <52f1c174-6af1-4fda-9ff8-a5e2cf507b5f@googlegroups.com> <5d94897b-fedc-4a28-81d0-3579e9bf754d@googlegroups.com> <4d53f1c5-b622-4425-9dfe-694c42676cae@googlegroups.com> <5d68da29-653c-42f5-8dbe-c11d3b304322@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="d0d5c56ca22fa2e528cedb366e6a556f"; logging-data="4039"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/uvflfq7sM/jkcRgiVS3nv" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:oA/LebYPeq/44G1tVpP+TGmoMAE= sha1:01o8h5n1qUWgyZNxjVBaJyj7mcM= Xref: csiph.com comp.lang.c:98892 BartC writes: > On 11/12/2016 16:22, Richard Heathfield wrote: [...] >> Do you really think that reading data from a stream is isomorphic to >> filling a series of glasses? > > So what /is/ it like then? You blindly keep on doing something until it > breaks and using that to tell you've finished (and it's time to pick up > the pieces). Yes. That's not as sensible solution if you're backing up a car or filling a series of glasses, because in those contexts a failure in one step can have harmful side effects. An input function that fails because there's no more data to read will report failure *without* doing any further damage. It is therefore sensible to attempt to read input and then check whether the attempt succeeded. > Sometimes there is no choice, but here there is. Sure, there's a choice. Checking for end-of-file at the top of the loop, given the way C's input functions are designed, is not the best one. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"