[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#1986: stdio broken? Strange behaviour of fgets() and scanf()



On Thu, 7 Dec 1995, brian (b.c.) white wrote:

> >  for (ksnum=0; 1; c=fgets(buf,sizeof(buf),stdin)) {
>
> The third thing in the "for" structure only gets executed at end of
> the loop.  'c' thus has an undefined value on the first iteration
> which just happened to be NULL for you (hence the "(nil)" output).
> GCC should have warned about this if you use "-Wall".

Whoops. Yes, that much is just me being an idiot. The bit about return
values from scanf still stands, though.

> Personally, I don't trust 'scanf' like this.  It always seems to get
> confused regarding end-of-line and so never scans where I want it to.
> I always use 'fgets' followed by 'sscanf'.  This, however, is not the
> basis of your problem.  Why you never get '-1' returned, I don't know.

The fgets() in the 'for' line seems to be to flush the remaining few
characters of the line before using scanf on the next line. Not quite how
I would have written it, but it's always worked up until this version of
libc.

Steve Early
sde1000@cam.ac.uk


Reply to: