fflush(stdin); 
Author Message
 fflush(stdin);

Al Bowers wrote the following in response to rupesh Reddy's post "FSCANF
PROBLEM:"

Quote:
> char *getname(char *b, size_t size) {
>    char *s;
>    int c;

>    if(fgets(b,size,stdin) == NULL) return NULL;
>    if((s = strchr(b,'\n')) != NULL) *s = '\0';
>    else while((c = getchar()) != '\n' || c != EOF);
>    return b;
>    }

I thought an easier way to do this would be to flush stdin, but I'm not sure
that's valid:

#include <stdio.h>
#include <string.h>

int
main(void)
{
    char buf[10];
    char *p;

    while(fgets(buf, sizeof buf, stdin)){
        if(p=strchr(buf, '\n'))
            *p = 0;
        else
            fflush(stdin);
        puts(buf);
    }
    return 0;

Quote:
}

        david
--
If 91 were prime, it would be a counterexample to your conjecture.
    -- Bruce Wheeler


Sat, 10 Jan 2004 21:46:01 GMT  
 fflush(stdin);
Hi David,


Quote:
> I thought an easier way to do this would be to flush stdin, but I'm not
> sure that's valid:

Fflushing is only defined for output(*), and toilets.

Daniel

(*) Output streams, or update streams for which the last operation was not
    input.

--
IMO, anyway.

` { } \ | [ ] '



Sat, 10 Jan 2004 22:01:25 GMT  
 fflush(stdin);

: I thought an easier way to do this would be to flush stdin, but I'm not sure
: that's valid:

It isn't. fflush(stdin) invokes explicitly undefined behaviour. (I
think it's mentioned in K&R2 as well as in the standard).

-- Mat.



Sat, 10 Jan 2004 22:11:34 GMT  
 fflush(stdin);

Quote:

> Al Bowers wrote the following in response to rupesh Reddy's post "FSCANF
> PROBLEM:"

> > char *getname(char *b, size_t size) {
> >    char *s;
> >    int c;

> >    if(fgets(b,size,stdin) == NULL) return NULL;
> >    if((s = strchr(b,'\n')) != NULL) *s = '\0';
> >    else while((c = getchar()) != '\n' || c != EOF);
> >    return b;
> >    }

> I thought an easier way to do this would be to flush stdin, but I'm not sure
> that's valid:

It's not. `flush' is only defined for output streams.

[snip]

HTH,
--ag

--
Artie Gold, Austin, TX  (finger the cs.utexas.edu account for more info)

--
"Wake me when it's funny."



Sat, 10 Jan 2004 22:10:44 GMT  
 fflush(stdin);
tahnks for solution and nice tips

Quote:

>Al Bowers wrote the following in response to rupesh Reddy's post "FSCANF
>PROBLEM:"

>> char *getname(char *b, size_t size) {
>>    char *s;
>>    int c;

>>    if(fgets(b,size,stdin) == NULL) return NULL;
>>    if((s = strchr(b,'\n')) != NULL) *s = '\0';
>>    else while((c = getchar()) != '\n' || c != EOF);
>>    return b;
>>    }

>I thought an easier way to do this would be to flush stdin, but I'm not sure
>that's valid:

>#include <stdio.h>
>#include <string.h>

>int
>main(void)
>{
>    char buf[10];
>    char *p;

>    while(fgets(buf, sizeof buf, stdin)){
>        if(p=strchr(buf, '\n'))
>            *p = 0;
>        else
>            fflush(stdin);
>        puts(buf);
>    }
>    return 0;
>}

>    david
>--
>If 91 were prime, it would be a counterexample to your conjecture.
>    -- Bruce Wheeler

--
Hemang Ajmera


Sat, 10 Jan 2004 23:01:06 GMT  
 fflush(stdin);

Quote:

> I thought an easier way to do this would be to flush stdin, but I'm not sure
> that's valid:

It doesn't even mean anything.


Sun, 11 Jan 2004 00:00:00 GMT  
 fflush(stdin);

Quote:

> I thought an easier way to do this would be to flush stdin, but I'm not sure
> that's valid:

<code snipped>

I am quite sure that it's /not/ valid.

You would be, too, had you read the FAQ:
http://www.eskimo.com/~scs/C-faq/q12.26.html

Micah

--
"You will be able to move on with your life.  You will never
forget what civility and common sense are.  They will always be with you,
but you will no longer be in mourning for them."



Sun, 11 Jan 2004 03:49:16 GMT  
 fflush(stdin);

Quote:

>: I thought an easier way to do this would be to flush stdin, but I'm not sure
>: that's valid:

>It isn't. fflush(stdin) invokes explicitly undefined behaviour. (I
>think it's mentioned in K&R2 as well as in the standard).

It's certainly mentioned in the FAQ, which is mandatory reading for the
participants to this newsgroup, even when their name is David Rubin.

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Sun, 11 Jan 2004 11:20:27 GMT  
 fflush(stdin);

Quote:

> It's certainly mentioned in the FAQ, which is mandatory reading for the
> participants to this newsgroup, even when their name is David Rubin.

Good point! Micah also pointed this out to me. Sorry for being lazy...

        david

--
If 91 were prime, it would be a counterexample to your conjecture.
    -- Bruce Wheeler



Sun, 11 Jan 2004 23:46:44 GMT  
 fflush(stdin);

Quote:

> It isn't. fflush(stdin) invokes explicitly undefined behaviour.
> (I think it's mentioned in K&R2 as well as in the standard).

Is it still undefined if I first freopen stdin as an output stream?


Mon, 12 Jan 2004 00:20:39 GMT  
 fflush(stdin);

Quote:

>> It isn't. fflush(stdin) invokes explicitly undefined behaviour.
>> (I think it's mentioned in K&R2 as well as in the standard).
> Is it still undefined if I first freopen stdin as an output stream?

I don't think so. There's nothing magical about the name "stdin" that
makes it unsuitable for fflushing, it's the fact that it's an input
stream that causes the undefined behaviour.
NB: I haven't read the standard, so take that into account when reading
my advice.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"Keep shooting, sooner or later you're bound to hit something."
   - Misfire



Mon, 12 Jan 2004 00:46:34 GMT  
 fflush(stdin);

Quote:

> > It isn't. fflush(stdin) invokes explicitly undefined behaviour.
> > (I think it's mentioned in K&R2 as well as in the standard).

> Is it still undefined if I first freopen stdin as an output stream?

Well, no, but that'd be some seriously twisted programming.  And how
would you then get user input?

Micah

--
"Just because Unix is the best operating system doesn't mean it is the
be-all and end-all of computer science. Err, or something."
                      - Joona I Palaste



Mon, 12 Jan 2004 03:32:41 GMT  
 fflush(stdin);

Quote:



> > > It isn't. fflush(stdin) invokes explicitly undefined behaviour.
> > > (I think it's mentioned in K&R2 as well as in the standard).

> > Is it still undefined if I first freopen stdin as an output stream?

> Well, no, but that'd be some seriously twisted programming.  And how
> would you then get user input?

Micah...

You're a great straight man...  By first freopening stdout as an input
stream!

8-)
--
Morris Dovey
West Des Moines, Iowa USA
Available for embedded C contract work



Mon, 12 Jan 2004 04:43:37 GMT  
 fflush(stdin);

Quote:



>> > > It isn't. fflush(stdin) invokes explicitly undefined behaviour.
>> > > (I think it's mentioned in K&R2 as well as in the standard).

>> > Is it still undefined if I first freopen stdin as an output stream?

>> Well, no, but that'd be some seriously twisted programming.  And how
>> would you then get user input?

>Micah...

>You're a great straight man...  By first freopening stdout as an input
>stream!

Using what file name? :-)

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Mon, 12 Jan 2004 09:40:05 GMT  
 fflush(stdin);

Quote:


> >Micah...

> >You're a great straight man...  By first freopening stdout as an input
> >stream!

> Using what file name? :-)

Dan...

How about \"*.*\"

8-)
--
Morris Dovey
West Des Moines, Iowa USA
Available for embedded C contract work



Mon, 12 Jan 2004 10:35:15 GMT  
 
 [ 19 post ]  Go to page: [1] [2]

 Relevant Pages 

1. fflush(stdin) - not guaranteed to work?

2. fflush(stdin) - not guaranteed to work?

3. reading 1 (and only 1) char - fflush(stdin)

4. fflush(stdin)

5. fflush(stdin) in C-FAQ

6. OT: glibc In Linux RedHat 6.2 and ANCI C fflush(stdin)

7. Why doen`t fflush (stdin) work ???

8. fflush(stdin) and gets()

9. fflush(stdin)

10. fflush(stdin) Not ANSI C ?

11. Why doesn't fflush(stdin) work right?

12. fflush(stdin) - not guaranteed to work?

 

 
Powered by phpBB® Forum Software