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: Sat, 10 Dec 2016 14:45:38 -0800 Organization: None to speak of Lines: 74 Message-ID: References: <52f1c174-6af1-4fda-9ff8-a5e2cf507b5f@googlegroups.com> <5d94897b-fedc-4a28-81d0-3579e9bf754d@googlegroups.com> <4d53f1c5-b622-4425-9dfe-694c42676cae@googlegroups.com> <2ce3a902-1b4f-4487-b4f8-a18eae15f453@googlegroups.com> <878trn3gss.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="d3e5cee4cb06db8dd070f0b5a238435d"; logging-data="28874"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/cYi1R+3SU3HriMmqYZc9J" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:BDiHUNnyCYQgDJ904y7N9mmr504= sha1:rROESQYXpiIZYtPPinj14hrRyIY= Xref: csiph.com comp.lang.c:98743 Ben Bacarisse writes: > Barry Schwarz writes: > >> On Sat, 10 Dec 2016 03:19:19 -0800 (PST), Alla _ >> wrote: >> >>>On Friday, December 9, 2016 at 10:39:29 PM UTC+3, Keith Thompson wrote: >>>> Alla _ writes: > >>>> > scanf( "%d%29s%lf", &account, name, &balance ); >>>> > >>>> > // write account, name and balance into file with fprintf >>>> > while ( !feof( stdin ) ) >>>> > { >>>> > fprintf( cfPtr, "%d %s %.2f\n", account, name, balance ); >>>> > > >>>> > >>>> > printf( "%s", "? " ); >>>> > scanf( "%d%29s%lf", &account, name, &balance ); >>>> > } // end while > > >>>> I have one very specific question about this. >>>> >>>> Does the sample program in Deitel&Deitel actually use >>>> "while ( !feof( stdin ) )"? >>>> >>>Yes. I have noted that I have posted the program written by D&D, and >>>have specified only one instance, where I have added something >>>different - it is not this while expression. >>> >>>> If so, though I'd hesitate to judge an entire book based on one >>>> line of code, that in particular is a strike against this one. >> >> Given that there is a call to scanf immediately prior to testing feof, >> the only thing wrong with the loop is it will fail if there is an I/O >> error. Yes, I think you're right. feof() returns true only after a failing scanf() call, which will return a value other than 3 (which the program quietly ignores). But the fact that it happens to work in this case does not, IMHO, justify teaching this particular bad habit. My judgement of the book would depend in part on how it discusses this issue later on. > "I/O error" is a very general term, but in the context of comp.lang.c it > might reasonably be taken to mean those errors detected by ferror(). > Those errors don't cause the loop to fail, of course, they cause it > simply to (correctly) end. Conversion errors, on the other hand, do > cause the loop to fail in the sense of "going horribly wrong". I'm not > sure if you were including conversion errors in "I/O errors" which I why > I've written a whole paragraph about stuff I'm sure you know! An I/O error of the kind detected by ferror() will cause an infinite loop. The loop is: while ( !feof( stdin ) ) /* ... */ If an error causes ferror(stdin) to return true, feof(stdin) will continue to return false. [...] -- 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"