linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* buggy_double_use_of _scanf
@ 2004-10-26 12:42 simon
  2004-10-26 20:59 ` Glynn Clements
  0 siblings, 1 reply; 5+ messages in thread
From: simon @ 2004-10-26 12:42 UTC (permalink / raw)
  To: linux-C-programming

hello,

I have observe a strange scanf behaviour...
when using two successive scanf... the second receive a return character

for example :

int a;
char b;

scanf ("%d", &a);
fflush (stdin);
scanf ("%c", &b);
fprintf (stdout, "a : %d\nb : %c\n", a, b);

what's the problem ?
is scanf using a buggy internal buffer ?

simon

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: buggy_double_use_of _scanf
  2004-10-26 12:42 buggy_double_use_of _scanf simon
@ 2004-10-26 20:59 ` Glynn Clements
  2004-11-01 19:03   ` Fábio Russo
  0 siblings, 1 reply; 5+ messages in thread
From: Glynn Clements @ 2004-10-26 20:59 UTC (permalink / raw)
  To: simon; +Cc: linux-C-programming


simon wrote:

> I have observe a strange scanf behaviour...
> when using two successive scanf... the second receive a return character
> 
> for example :
> 
> int a;
> char b;
> 
> scanf ("%d", &a);
> fflush (stdin);
> scanf ("%c", &b);
> fprintf (stdout, "a : %d\nb : %c\n", a, b);
> 
> what's the problem ?

What makes you think that there is a problem?

What's the input? If it's a decimal number followed by newline, the
first scanf() will return the parsed number, the second will return
the newline.

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: buggy_double_use_of _scanf
  2004-10-26 20:59 ` Glynn Clements
@ 2004-11-01 19:03   ` Fábio Russo
  2004-11-02  3:44     ` Glynn Clements
  0 siblings, 1 reply; 5+ messages in thread
From: Fábio Russo @ 2004-11-01 19:03 UTC (permalink / raw)
  To: Glynn Clements, simon; +Cc: linux-C-programming


----- Original Message -----
From: "Glynn Clements" <glynn@gclements.plus.com>
To: "simon" <simon.guinot@laposte.net>
Cc: "linux-C-programming" <linux-c-programming@vger.kernel.org>
Sent: Tuesday, October 26, 2004 6:59 PM
Subject: Re: buggy_double_use_of _scanf


>
> simon wrote:
>
> > I have observe a strange scanf behaviour...
> > when using two successive scanf... the second receive a return character
> >
> > for example :
> >
> > int a;
> > char b;
> >
> > scanf ("%d", &a);
> > fflush (stdin);
> > scanf ("%c", &b);
> > fprintf (stdout, "a : %d\nb : %c\n", a, b);
> >
> > what's the problem ?
>
> What makes you think that there is a problem?
>
> What's the input? If it's a decimal number followed by newline, the
> first scanf() will return the parsed number, the second will return
> the newline.


The problem, I beleve is in the fflush function. I Have the same behaviour
with this source code,
but when I put a additional scanf in the line, all works fine -;)

Now I ask:
Why the fflush function did not remove the new line caracter when it should
?

it´s all folks !!!

        Russo.


-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: buggy_double_use_of _scanf
@ 2004-11-01 22:25 Huber, George K RDECOM CERDEC STCD SRI
  0 siblings, 0 replies; 5+ messages in thread
From: Huber, George K RDECOM CERDEC STCD SRI @ 2004-11-01 22:25 UTC (permalink / raw)
  To: 'Fábio Russo', Glynn Clements, simon; +Cc: linux-C-programming

Hi,

Russo wrote:

[... snip ...]

>The problem, I beleve is in the fflush function. I Have the same behaviour
>with this source code, but when I put a additional scanf in the line, all 
>works fine -;)

>Now I ask:
>Why the fflush function did not remove the new line caracter when it
should?

quoteth the man page:

'The function fflush forces a write of all user-space buffered data for
 the given output or update stream via the stream's underlying write 
 function.'

Thus fflush only causes user-space buffered data to write their contents to 
the kernel-space buffers (which can be forced to update to the storage
medium
via a call to sync or fsync).  fflush has no effect when called on stdin - 
you are reading data not writing it.

George

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: buggy_double_use_of _scanf
  2004-11-01 19:03   ` Fábio Russo
@ 2004-11-02  3:44     ` Glynn Clements
  0 siblings, 0 replies; 5+ messages in thread
From: Glynn Clements @ 2004-11-02  3:44 UTC (permalink / raw)
  To: Fábio Russo; +Cc: simon, linux-C-programming


Fábio Russo wrote:

> > > I have observe a strange scanf behaviour...
> > > when using two successive scanf... the second receive a return character
> > >
> > > for example :
> > >
> > > int a;
> > > char b;
> > >
> > > scanf ("%d", &a);
> > > fflush (stdin);
> > > scanf ("%c", &b);
> > > fprintf (stdout, "a : %d\nb : %c\n", a, b);
> > >
> > > what's the problem ?
> >
> > What makes you think that there is a problem?
> >
> > What's the input? If it's a decimal number followed by newline, the
> > first scanf() will return the parsed number, the second will return
> > the newline.
> 
> 
> The problem, I beleve is in the fflush function. I Have the same behaviour
> with this source code,
> but when I put a additional scanf in the line, all works fine -;)
> 
> Now I ask:
> Why the fflush function did not remove the new line caracter when it should
> ?

fflush() should not have removed any characters from the buffer. The
purpose of fflush() is to ensure that any data in *output* buffers is
written to the underlying descriptor.

If your input consists of lines, you should probably be using fgets()
to read each line then using sscanf() to parse the text, rather than
using scanf() (or fscanf()) directly.

For a start, using fgets() makes it much easier to recover from
errors. If you call scanf(), and it indicates that it didn't read
everything which you wanted it to, it can be unclear as to exactly
what it did read and what is likely to be left in the buffer.

-- 
Glynn Clements <glynn@gclements.plus.com>
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-11-02  3:44 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-10-26 12:42 buggy_double_use_of _scanf simon
2004-10-26 20:59 ` Glynn Clements
2004-11-01 19:03   ` Fábio Russo
2004-11-02  3:44     ` Glynn Clements
  -- strict thread matches above, loose matches on Subject: below --
2004-11-01 22:25 Huber, George K RDECOM CERDEC STCD SRI

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).