Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

This should be easy! fscanf problems with EOF

1,552 views
Skip to first unread message

Jonathan Armstrong

unread,
Sep 8, 1997, 3:00:00 AM9/8/97
to

--------------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>&nbsp;
<UL>&nbsp;FILE *file;
<BR>&nbsp;

<P>if((file = fopen(filename, "r")) == NULL){
<BR>&nbsp;&nbsp;&nbsp; printf("Sorry - unable to open file");
<BR>&nbsp;&nbsp;&nbsp; exit (1);
<BR>}

<P>while ((fscanf(file,"%c",&amp;instruction)) !=EOF){
<BR>&nbsp;&nbsp;&nbsp; blah blah
<BR>&nbsp;&nbsp;&nbsp; fclose (file);

<P>&nbsp;&nbsp;&nbsp; scanf("%c",&amp;wait);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
/* otherwise the program closes before I can see it! */</UL>
}

<P>Thanks in advance,

<P>Jonathan Armstrong
<BR>Sydney, Australia.
<UL>&nbsp;
<BR>&nbsp;</UL>
</HTML>

--------------17BF13CCEE5474CE1595E747--


Morris M. Keesan

unread,
Sep 8, 1997, 3:00:00 AM9/8/97
to

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.

Alicia Carla Longstreet

unread,
Sep 8, 1997, 3:00:00 AM9/8/97
to Jonathan Armstrong

Jonathan Armstrong wrote:

> 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 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

Morris M. Keesan

unread,
Sep 8, 1997, 3:00:00 AM9/8/97
to

On Mon, 08 Sep 1997 14:34:32 -0400, Alicia Carla Longstreet
<ca...@ici.net> wrote:
>Jonathan Armstrong wrote:
...

>> while ((fscanf(file,"%c",&instruction)) !=EOF){
...

> In any event, when your fscanf() function reaches the EOF,
>it puts 0xFF into instruction, which it then compares to 0xFFFF.
No, it doesn't. When fscanf reaches end of file, it returns EOF,
without modifying instruction.
We've all gotten so accustomed to correcting people who store the result
of getchar() in a char instead of an int, that we're seeing that error
in places that have nothing to do with the value of EOF and whether it
can be stored in a char.

George Swan

unread,
Sep 8, 1997, 3:00:00 AM9/8/97
to

>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) ...


Magnus Lööf

unread,
Sep 8, 1997, 3:00:00 AM9/8/97
to

On Mon, 08 Sep 1997 16:30:25 +1100, Jonathan Armstrong <j...@wr.com.au>
wrote:

>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"

Brent Nelson

unread,
Sep 8, 1997, 3:00:00 AM9/8/97
to

Jonathan Armstrong wrote:
>
> Dear All,
>
> Can anyone tell me why fscanf crashes my program when it gets to EOF?
> My code says:
>
> while ((fscanf(file,"%c",&instruction)) !=EOF){
> blah blah
> fclose (file);

>
> scanf("%c",&wait); /* otherwise the program closes
> before I can see it! */
>
> }
>

Jonathan -

Please tell me the fclose isn't inside the while loop.

--
Brent Nelson

e96...@edu.tut.ac.jp

unread,
Sep 9, 1997, 3:00:00 AM9/9/97
to

>>>>> "Jonathan" == Jonathan Armstrong <j...@wr.com.au> writes:


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

Morris M. Keesan

unread,
Sep 9, 1997, 3:00:00 AM9/9/97
to

On 8 Sep 1997 17:32:27 -0400, gs...@globalserve.net (George Swan) wrote:
>>Jonathan Armstrong wrote:
...
>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.
Indeed, conforming compilers do that when comparing signed quantities,
and therefore (int)(-1) == (signed char)(-1), but
(int)(-1) != (unsigned char)(-1), and for historical reasons, the
language does not specify the signedness of "plain" characters. So
perhaps we should write (int)(-1) x= (char)(-1), where x= can be read as
"not portably equal -- check your implementation definition".

Damir Zucic

unread,
Sep 9, 1997, 3:00:00 AM9/9/97
to

Jonathan Armstrong (j...@wr.com.au) wrote:

: 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! */


Morris M. Keesan

unread,
Sep 9, 1997, 3:00:00 AM9/9/97
to

On 9 Sep 1997 13:41:01 GMT, zu...@unios.hr (Damir Zucic) wrote:
>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! */
>
In my opinion, it's NOT a good idea to use ferror/feof after every
fgets. It's common usage among neos, either new programmers or people
switching to C from another language, but most experienced C programmers
will only call ferror and/or feof after the return value from fgets
indicates that it's necessary. fgets returns a null pointer on EOF or
error, and to quote from "man fgets" on my HP/UX system,
"ferror() and feof() can be used to distinguish between an error
condition and an end-of-file condition."

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 {...}


Magnus Lööf

unread,
Sep 9, 1997, 3:00:00 AM9/9/97
to

On Mon, 08 Sep 1997 17:31:57 GMT, mke...@kenan.com (Morris M. Keesan)
wrote:

>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.

Ralph Silverman

unread,
Sep 19, 1997, 3:00:00 AM9/19/97
to

Morris M. Keesan (mke...@kenan.com) wrote:


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


0 new messages