PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Question on while(!feof(fp))
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Question on while(!feof(fp))

Réponse
 
LinkBack Outils de la discussion
Vieux 30/01/2008, 18h27   #1
Willem
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Question on while(!feof(fp))


As we all know, the following code is not quite correct:

while (!feof(fp)) { fgets(...); /* do_something */ }

And should be replaced by:

while (fgets(...)) { /* do_something */ }


But can the faulty code have the result that the loop body is *not*
called, even though there is data in the file ?

I'd say no, because there hasn't been anything yet that could have set the
end-of-file indicator, but I have a program which copies data and once in
a while, it produices an empty file.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
  Réponse avec citation
Vieux 30/01/2008, 18h46   #2
Robert Gamble
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question on while(!feof(fp))

On Jan 30, 1:27 pm, Willem <wil...@stack.nl> wrote:
> As we all know, the following code is not quite correct:
>
> while (!feof(fp)) { fgets(...); /* do_something */ }
>
> And should be replaced by:
>
> while (fgets(...)) { /* do_something */ }
>
> But can the faulty code have the result that the loop body is *not*
> called, even though there is data in the file ?
>
> I'd say no, because there hasn't been anything yet that could have set the
> end-of-file indicator, but I have a program which copies data and once in
> a while, it produices an empty file.


The eof indicator for a stream is always cleared after successfully
opening a file so if no other file operations took place on the stream
before the call to feof() it couldn't return true. In the case of an
empty file, the the eof indicator wouldn't get set until a read was
attempted.

--
Robert Gamble
  Réponse avec citation
Vieux 31/01/2008, 02h17   #3
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question on while(!feof(fp))

Willem wrote:
>
> As we all know, the following code is not quite correct:
>
> while (!feof(fp)) { fgets(...); /* do_something */ }
>
> And should be replaced by:
>
> while (fgets(...)) { /* do_something */ }


Try "while (!fgets(...)) {/* do_something */}". It'll work better.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


--
Posted via a free Usenet account from http://www.teranews.com

  Réponse avec citation
Vieux 31/01/2008, 05h54   #4
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question on while(!feof(fp))

CBFalconer <cbfalconer@yahoo.com> writes:
> Willem wrote:
>> As we all know, the following code is not quite correct:
>>
>> while (!feof(fp)) { fgets(...); /* do_something */ }
>>
>> And should be replaced by:
>>
>> while (fgets(...)) { /* do_something */ }

>
> Try "while (!fgets(...)) {/* do_something */}". It'll work better.


Um, how so? fgets() returns its first argument on success, or a null
pointer if nothing is read.

--
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  Réponse avec citation
Vieux 01/02/2008, 00h19   #5
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question on while(!feof(fp))

Keith Thompson wrote:
> CBFalconer <cbfalconer@yahoo.com> writes:
>> Willem wrote:
>>

.... snip ...
>>>
>>> while (fgets(...)) { /* do_something */ }

>>
>> Try "while (!fgets(...)) {/* do_something */}". It'll work better.

>
> Um, how so? fgets() returns its first argument on success, or a
> null pointer if nothing is read.


Um. I assumed do_something fixed up the error. You didn't.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

  Réponse avec citation
Vieux 01/02/2008, 02h22   #6
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question on while(!feof(fp))

CBFalconer <cbfalconer@yahoo.com> writes:
> Keith Thompson wrote:
>> CBFalconer <cbfalconer@yahoo.com> writes:
>>> Willem wrote:
>>>

> ... snip ...
>>>>
>>>> while (fgets(...)) { /* do_something */ }
>>>
>>> Try "while (!fgets(...)) {/* do_something */}". It'll work better.

>>
>> Um, how so? fgets() returns its first argument on success, or a
>> null pointer if nothing is read.

>
> Um. I assumed do_something fixed up the error. You didn't.


An odd assumption on your part, IMHO. The original question was about
while (!feof(fp)) { fgets(...); /* do_something */ }
vs.
while (fgets(...)) { /* do_something */ }
It seemed fairly obvious to me that do_something handles the line read
by fgets(); any error handling would happen after the loop.

--
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h41.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14960 seconds with 14 queries