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: Fri, 09 Dec 2016 11:40:28 -0800 Organization: None to speak of Lines: 63 Message-ID: References: <52f1c174-6af1-4fda-9ff8-a5e2cf507b5f@googlegroups.com> <5d94897b-fedc-4a28-81d0-3579e9bf754d@googlegroups.com> <4d53f1c5-b622-4425-9dfe-694c42676cae@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="2ad4785427d430f26583540ef5e88668"; logging-data="8777"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+3HdNwSwhQdO3McCCN6EZH" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:/XqNeponJ+gwzV0zEcY1NkZgX/U= sha1:L/J6B3gEbob/e25kv/eQK+MRgfE= Xref: csiph.com comp.lang.c:98603 Alla _ writes: [...] > ( I ) two original programs: one creates a file, another reads from that file. > > // Fig. 11.2: fig11_02.c > // Creating a sequential file > > #include > > int main( void ) > { > unsigned int account; // account number > char name[ 30 ]; // account name > double balance; // account balance > > FILE *cfPtr; // cfPtr = clients.csv file pointer > > // fopen opens file. Exit program if unable to create file > if ( (cfPtr = fopen( "clients.csv", "w")) == NULL ) > { > puts( "File could not be opened" ); > } // end if > else > { > puts( "Enter the account, name, and balance." ); > puts( "Enter EOF to end input." ); > printf( "%s", "? " ); > 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 ); > > /** > fprintf( stdout, "%d %s %.2f\n", account, name, balance ); > to print the data to the standard output > **/ > > printf( "%s", "? " ); > scanf( "%d%29s%lf", &account, name, &balance ); > } // end while > > fclose( cfPtr ); // fclose closes file > > } // end else > } // end main [...] I have one very specific question about this. Does the sample program in Deitel&Deitel actually use "while ( !feof( stdin ) )"? 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. -- 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"