[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] End of file? (Beginning of file??)



Hans Henrik Hansen <sslug@sslug> writes:

> I mit - tidligere omtalte - lille C++ kursus er jeg nu nået til en lektion, 
> hvor jeg skal lære at hente (numeriske) data fra en separat fil ind i et 
> program og bearbejde dem lidt. Opgavens ordlyd er:

[...]

> Dernæst har jeg (med lidt bistand fra bogen) lavet flg. program:
> 	while( !infile.eof() )
>   	{
> 		count++;
>     		infile >> price; // follow-up read
> 		sum += price; // add the current price to the total

Metoden .eof() returnerer true hvis en *tidligere* IO operation har
nået EOF -- ikke hvis den *næste* IO operation vil fejle.

I stedet bør du omskrive din løkke til:

// Sålænge der kunne indlæses en pris
while (infile >> price) {
  // behandl price
  ...
}

som også vil håndtere andre fejl.

Det samme gælder iøvrigt for funktionen feof() i C biblioteket -- jeg
ser den også ofte forkert brugt på denne måde.

-- 
===============================================================
<sslug@sslug>                           Herlev, Denmark     
<URL:http://www.andreasen.org/>                             <*>   
===============================================================



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:09 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *