[comp.lang.c] fscanf & EOF

gls@novavax.UUCP (Gary Schaps) (02/11/91)

Would some kind soul care to illuminate for me why this code dumps core?

	 while ( (fscanf(input,"%8s%1s%2s", part_number, delimiter, 
		  part_class)) && (feof(input)) ) 

Thanks.

-- 

Gary L. Schaps	
gls@novavax.nova.edu

kelley@aclcb.purdue.edu (Steve Kelley) (02/12/91)

In article <2145@novavax.UUCP>, gls@novavax.UUCP (Gary Schaps) writes:
-Would some kind soul care to illuminate for me why this code dumps core?
-
-	 while ( (fscanf(input,"%8s%1s%2s", part_number, delimiter, 
-		  part_class)) && (feof(input)) ) 
-

	Because it's wrong.

-Thanks.


	You're welcome.

newberry@nmsu.edu (Jeff Newberry) (02/12/91)

>Would some kind soul care to illuminate for me why this code dumps core?

>	 while ( (fscanf(input,"%8s%1s%2s", part_number, delimiter, 
>		  part_class)) && (feof(input)) ) 


How are the arguments defined?  If they are all defined as arrays of
characters, then I don't know why it core dumps.  If "delimiter" is
defined as a charater, then you need "&delimiter".

Also, I assume you want to put (!feof(input))?

I hope this helps.

Jeff Newberry
newberry%nmsu.edu

fenn@wpi.WPI.EDU (Brian Fennell) (02/12/91)

In article <NEWBERRY.91Feb11114703@illyria.nmsu.edu> 
	newberry@nmsu.edu (Jeff Newberry) writes:
>
>How are the arguments defined?  If they are all defined as arrays of
>characters, then I don't know why it core dumps.  If "delimiter" is
>defined as a charater, then you need "&delimiter".

If your delimiter is a character, it shouldn't be.  &delimiter will
work only if you are lucky enough to have (&delimiter)+1 empty, or
empty-able because it will get the '\0' terminating your 1 character
string.

karl@ima.isc.com (Karl Heuer) (02/12/91)

In article <1991Feb11.213431.9260@wpi.WPI.EDU> fenn@wpi.WPI.EDU (Brian Fennell) writes:
>[You can't use scanf("%1s", ch) where ch is just a char; it needs an array of
>at least two characters, since it will try to store the '\0' too.]

Right.  This is a long-standing bug in the man page for scanf(), which claims
that "%1s" is a good substitute for "%c" if you want to force it to skip
initial whitespace.  (The correct substitute is " %c".)

Karl W. Z. Heuer (karl@ima.isc.com or uunet!ima!karl), The Walking Lint

newberry@nmsu.edu (Jeff Newberry) (02/13/91)

>>
>>How are the arguments defined?  If they are all defined as arrays of
>>characters, then I don't know why it core dumps.  If "delimiter" is
>>defined as a charater, then you need "&delimiter".

>If your delimiter is a character, it shouldn't be.  &delimiter will
>work only if you are lucky enough to have (&delimiter)+1 empty, or
>empty-able because it will get the '\0' terminating your 1 character
>string.

Pleas forgive me for my incorrect information.  Brian Fennell is
absolutely correct.  Thank you for correcting me.

Jeff Newberry

s64421@zeus.usq.EDU.AU (house ron) (02/15/91)

gls@novavax.UUCP (Gary Schaps) writes:

>Would some kind soul care to illuminate for me why this code dumps core?

>	 while ( (fscanf(input,"%8s%1s%2s", part_number, delimiter, 
>		  part_class)) && (feof(input)) ) 

Not unless you give the whole program.

--
Regards,

Ron House.   (s64421@zeus.usq.edu.au)
(By post: Info Tech, U.C.S.Q. Toowoomba. Australia. 4350)

james@castle.ed.ac.uk (J Gillespie) (02/20/91)

gls@novavax.UUCP (Gary Schaps) writes:

>Would some kind soul care to illuminate for me why this code dumps core?

>	 while ( (fscanf(input,"%8s%1s%2s", part_number, delimiter, 
>		  part_class)) && (feof(input)) ) 

Should that be feof(input), or !feof(input) ?
                               ^^^^^

-- 
  James Gillespie,     /~~~~~~~~\
 Edinburgh University /   @  @   \ "Oh wow.  What a bummer."
   james@ed.ac.uk    /     <      \		-- Truman Sparks, _Fandango_
____________________/  \________/  \__________________________________________