delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2004/08/12/19:00:24

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
X-Trace-PostClient-IP: 68.147.131.211
From: Brian Inglis <Brian DOT Inglis AT SystematicSw DOT Invalid>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: program hanging
Organization: Systematic Software
Message-ID: <8crnh0p8uhi20ljernhr8un2hntpvrgb70@4ax.com>
References: <1092258885 DOT 100659 AT proxy2 DOT srv DOT ualberta DOT ca> <7105-Thu12Aug2004064431+0300-eliz AT gnt DOT org> <1092324277 DOT 615039 AT proxy2 DOT srv DOT ualberta DOT ca> <sv9nh0tndcdjgritie7gqkjth48290ftoe AT 4ax DOT com> <1092348061 DOT 601537 AT proxy2 DOT srv DOT ualberta DOT ca>
X-Newsreader: Forte Agent 1.93/32.576 English (American)
MIME-Version: 1.0
Lines: 60
Date: Thu, 12 Aug 2004 22:48:46 GMT
NNTP-Posting-Host: 24.71.223.147
X-Complaints-To: abuse AT shaw DOT ca
X-Trace: pd7tw2no 1092350926 24.71.223.147 (Thu, 12 Aug 2004 16:48:46 MDT)
NNTP-Posting-Date: Thu, 12 Aug 2004 16:48:46 MDT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On Thu, 12 Aug 2004 16:02:14 -0600 in comp.os.msdos.djgpp, "John
Hanley" <jdhanley AT telusplanet DOT net> wrote:

>> >      rtn=fscanf(f,"%c",&c);
>>
>> What happens if you get an EOF?
>> You just go ahead and process whatever is in c anyway!
>
>I think I misinterpretted this when replying to your post.  You are right
>in that if it's EOF, I am trying to process it and it's not working so
>well because it's reading in a char and EOF is an int.

No, the problem is that fscanf() does *not* store a character into c
when EOF is detected!  

And the while loop does not test rtn for EOF until you reach the
bottom and loop back to the condition test at the top. 
But before you get to the bottom to loop and perform the EOF test, you
test the value in c. 
If EOF is detected by fscanf(), the value in c remains untouched, and
it may be set to the previous char, or may be set to some other random
value. 
If that random value in c just happens to be a newline \n or alnum,
you then proceed to *overwrite* the value of rtn from fcanf() with a
flag value of 0 or 1, break out of the while loop, and return the flag
value, not EOF!  

If you receive an EOF from fscanf(), and the "random" value of c just
happens to look like a newline \n, you will return 0 to the caller. 
If the caller happens to be your gobble newlines loop, it may never
terminate once EOF has been detected, as it is likely that the value
in c on the next call will be the same as on the previous call. 

>I tried an fgetc() and that worked just fine.

Calling fgetc() has the advantage that you either get a valid
character value or EOF, so the two are not separated, which will
ensure that the newline \n and isalnum() tests fail, so you reach the
while condition and terminate. 
If you perform the I/O and test together in the while condition, this
has the advantage that those two operations are not separated, and you
will not execute any code within the while loop when EOF is received. 

>Sorry for the confusion.  I didn't exactly understand what you were saying
>here the first time.

Nor the second time, it appears. 
I hope my more detailed explanation of your program's operation has
clarified your understanding of why your program appeared to hang, and
why it is useful to follow the usual idiom for performing I/O in C. 
You can also follow the Pascal I/O idiom, whereby the I/O calls are
duplicated just before entry to the while loop, and also just before
the end of the while loop, but C's expression syntax allows you to
eliminate that duplication, which can be a source of errors. 

-- 
Thanks. Take care, Brian Inglis 	Calgary, Alberta, Canada

Brian DOT Inglis AT CSi DOT com 	(Brian dot Inglis at SystematicSw dot ab dot ca)
    fake address		use address above to reply

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019