• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
C again
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

C again


  • Subject: C again
  • From: David Thorp <email@hidden>
  • Date: Sun, 31 Aug 2003 08:57:34 +1000

Hi again all,

I'm going nuts again. Would anyone be able to tell me why...

Given:

char PEEK (FILE *theFile)
{
char ch = fgetc (theFile);
ungetc (ch, theFile);
return ch;
}

and:

int error = 0;
char ch;
FILE *myFile = fopen ("<filename>", "r");

this evaluates to true, thus stepping into the loop (as expected):

while ((ch = PEEK (myFile)) != EOF)

but this evaluates to false and skips the loop altogether! (NOT
expected):

while (((ch = PEEK (myFile)) != EOF) && (error = 0))


As I see it, "(ch = ... != EOF)" is the same in both cases, and since
it evaluates to true in the first case it must be doing so in the
second, but the difference in the second case, "(error = 0)" is causing
the second while statement to evaluate false. But surely "int error =
0;" ensures that error is in fact 0!

Note they're not following on from each other in the code or anything
like that. I've got both lines in my code as above, one commented the
other not. I toggle which one is commented to test, and I get the
results as mentioned above.

What is going on?! Thanks in advance to anyone who can solve this
mystery for me! ;-)

David.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: C again
      • From: Angela Brett <email@hidden>
    • Re: C again
      • From: Andrew Pinski <email@hidden>
  • Prev by Date: Enough with the newbie C questions!
  • Next by Date: Re: C
  • Previous by thread: Re: Enough with the newbie C questions!
  • Next by thread: Re: C again
  • Index(es):
    • Date
    • Thread