--------------17BF13CCEE5474CE1595E747
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Dear All,
Can anyone tell me why fscanf crashes my program when it gets to EOF?
My code says:
FILE *file;
if((file = fopen(filename, "r")) == NULL){
printf("Sorry - unable to open file");
exit (1);
}
while ((fscanf(file,"%c",&instruction)) !=EOF){
blah blah
fclose (file);
scanf("%c",&wait); /* otherwise the program closes
before I can see it! */
}
Thanks in advance,
Jonathan Armstrong
Sydney, Australia.
--------------17BF13CCEE5474CE1595E747
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
<HTML>
Dear All,
<P>Can anyone tell me why fscanf crashes my program when it gets to EOF?
My code says:
<BR>
<UL> FILE *file;
<BR>
<P>if((file = fopen(filename, "r")) == NULL){
<BR> printf("Sorry - unable to open file");
<BR> exit (1);
<BR>}
<P>while ((fscanf(file,"%c",&instruction)) !=EOF){
<BR> blah blah
<BR> fclose (file);
<P> scanf("%c",&wait);
/* otherwise the program closes before I can see it! */</UL>
}
<P>Thanks in advance,
<P>Jonathan Armstrong
<BR>Sydney, Australia.
<UL>
<BR> </UL>
</HTML>
--------------17BF13CCEE5474CE1595E747--
EOF is an int value, on most systems it is (-1), an integer value
that can not be contained in a char. On a two's complement machine -1
would be 0xFFFF for an int but only 0xFF for a char, on a one's
complement machine -1 would be 0xfffe for an int but only 0xFE for a
char (I beleive this is correct). The basic concepet here is that
(int)(-1) != (char)(-1), at least as far as the bit pattern is
concerned. In any event, when your fscanf() function reaches the EOF,
it puts 0xFF into instruction, which it then compares to 0xFFFF. Since
these are two different values your while does not end (infinite loop)
and it merrily tries to read past the end of file which is causing your
program to crash.
I would not use fcanf() anyway, there is little or no error recovery,
try this:
int c;
char instruction;
while( (c=fgetc( file ))!=EOF) {
instruction = c;
/* blah blah */
}
> blah blah
> fclose (file);
I do not know if this is a typo or not, but it appears that you fclose()
is inside your while statement. If so, the above problem will never
crop up since your program will fail on the second iteration of the loop
when you fcanf() function attempts to read from a closed file. do make
sure your fclose() statement is NOT inside your while() loop.
> scanf("%c",&wait); /* otherwise the program closes
> before I can see it! */
> }
--
************************************************
* Alicia Carla Longstreet ca...@ici.net *
* Remove NO_SPAM when replying to me. *
************************************************
My compassion for someone is not limited
to my estimate of their intelligence.
Dr Gillian, Star Trek IV, The Voyage Home
************************************************
My opinion is a view I hold until - well - until
I find out something that changes it.
Luigi Pirandello
>Jonathan Armstrong wrote:
>> Can anyone tell me why fscanf crashes my program when it gets to EOF?
>> My code says:
>> while ((fscanf(file,"%c",&instruction)) !=EOF){
In article <341445...@ici.net>,
Alicia Carla Longstreet <carlaN...@ici.net> wrote:
>EOF is an int value, on most systems it is (-1), an integer value
>that can not be contained in a char. On a two's complement machine -1
>would be 0xFFFF for an int but only 0xFF for a char...
>The basic concepet here is that
>(int)(-1) != (char)(-1), at least as far as the bit pattern is
>concerned.
I believe Alicia is mistaken here. I believe conforming compilers
are smart enough to do sign extension when comparing signed quantities
of different sizes.
>In any event, when your fscanf() function reaches the EOF,
>it puts 0xFF into instruction, which it then compares to 0xFFFF.
I know that Alicia is mistaken here. There are two possible values
fscanf() could return here. If it returns a 1, it means 1 item
was successfully scanned, in which case a character gets stuffed
into the variable named "instruction". Alternatively, if it
returns EOF, nothing should have been scanned into instruction.
>I would not use fcanf() anyway, there is little or no error recovery,
>try this: [example using fgetc() snipped...]
IMO
Scanf() isn't so bad. But you've got to use it properly.
I've seen a lot of code where people don't
use it properly. I've seen programs which used fscanf() to read in
both numeric and non-numeric user input, where the programmer only
checked the return value of fscanf() to see if it was EOF. There
would be stuff like:
if (fscanf( in, "%d%d%d%d", & a [0], & a [1], & a [2], & a [3] ) == EOF)
return 0;
if (fscanf( in, "%s", filename ) == EOF) return 0;
The first fscanf() here could have returned EOF, or it might have
returned 0, 1, 2, 3 or 4. By not checking the programmers were
putting their programs at risk of running with garbage data.
This works better.
nread = fscanf( in, "%d%d%d%d", & a [0], & a [1], & a [2], & a [3] );
if (nread != 4) ...
>Dear All,
>
>Can anyone tell me why fscanf crashes my program when it gets to EOF?
>My code says:
>
>
> FILE *file;
>
>
> if((file = fopen(filename, "r")) == NULL){
> printf("Sorry - unable to open file");
> exit (1);
> }
>
> while ((fscanf(file,"%c",&instruction)) !=EOF){
^^
EOF can not be stored in a char variable because it's not a character.
Use int instead. Also, there's no declaration of variable
"instruction", so the program won't find any place to put the value
read from file. Doesn't your compiler warn you about this?
> blah blah
> fclose (file);
You don't want to close the file while working with it. put fclose
outside the loop.
> scanf("%c",&wait); /* otherwise the program closes
> before I can see it! */
I'm not sure what you're trying to do here, but you should at least
declare the variable wait.
>
>}
>
>Thanks in advance,
>
>Jonathan Armstrong
>Sydney, Australia.
It's quite hard to debug an uncomplete sourcecode, but I've done my
best. Hope it helps.
Magnus Lööf mag...@algonet.se
...and remember folks:
"There's no winner in a flame war"
Jonathan -
Please tell me the fclose isn't inside the while loop.
--
Brent Nelson
Jonathan> Dear All,
Jonathan> Can anyone tell me why fscanf crashes my program when it gets to EOF?
Jonathan> My code says:
Jonathan> FILE *file;
Jonathan> if((file = fopen(filename, "r")) == NULL){
Jonathan> printf("Sorry - unable to open file");
Jonathan> exit (1);
Jonathan> }
Jonathan> while ((fscanf(file,"%c",&instruction)) !=EOF){
Jonathan> blah blah
Jonathan> fclose (file);
Jonathan> scanf("%c",&wait); /* otherwise the program closes
Jonathan> before I can see it! */
Jonathan> }
Jonathan> Thanks in advance,
Jonathan> Jonathan Armstrong
Nobody can give you proper answer unless you clarify what you are doing
behind the "blah blah." There may often be pointer-related mistakes.
Besides, curly-brace is not properly matching. According to the indention,
fclose is inside the loop. This means you try to close the file every time
it succeeded getting a character.
yone
: Can anyone tell me why fscanf crashes my program when it gets to EOF?
: while ((fscanf(file,"%c",&instruction)) !=EOF){
Hi,
besides the misplaces fclose, it is generally beter to use fgets to read a line
from the text file. After that, you parse it with strtok, or sscanf.
And after every fgets, it is good idea to use ferror and feof to see if an
error occured, or eof was reached. Something like:
if (ferror (...))
{
if (feof (...)) break /* you can break from while (1) loop, this one */
/* never ends! */
The common idiom is
while(fgets(...) != NULL) {...}
if(ferror(...)) {/*handle error*/}
/* else normal EOF */
or, if you have a reason for doing while(1),
while(1) /* or for(;;) if you prefer */
{
...
if(fgets(...) == NULL) break;
...
}
if (ferror(...)) {...} else {...}
>On Mon, 08 Sep 1997 16:47:17 GMT, mag...@algonet.se (Magnus Lööf)
>wrote:
>>> while ((fscanf(file,"%c",&instruction)) !=EOF){
>> ^^
>>EOF can not be stored in a char variable because it's not a character.
>>Use int instead.
>Irrelevant. fscanf() is not getc() -- it returns EOF on end of file.
>It doesn't store out-of-bandwith values in its output variables.
Yes. You're right, of course. My mistake.
traditionally,
EOF
was associated with
file io
with unix ascii
( 7 significant digit ! )
...
return type of
getchar()
was ( common, signed )
int
( 16, or 32, digit )
...
in operation ...
getchar()
would return ascii value,
next character,
cast to
^^^^^^^ int
problem here was ...
how to have distinct
EOF
value ...
not equivalent to any
^^^
character !
--
Ralph Silverman
z007...@bcfreenet.seflin.lib.fl.us