fflush()



I use this function quite a lot. Recently one of my friend told me it is not good to use fflush().  

I googled it, and found that it gives some kind of unexpected behaviour. My question is when does this function works improperly and why?


Nowadays I use scanf("[^\n]") to flush input stream. Is this ok?
> 
> My question is when does this function works improperly and why?
> 
> 
> 


Technically it works improperly when you call it with an input stream or an update stream in input mode. That is, if you use fopen() with a "w" mode, or a "+" mode and you're currently in the process of reading from the stream, or stdin.


In practice, all of the above situations are okie dokie *iff* the compiler supports them (and some do, including Borland and Microsoft compilers). In terms of best practice, fflush() on an input stream is frowned upon.


However, note that calling fflush() on an output stream is both well defined and recommended for things such as forcing a user prompt to display, well...promptly. :)



> 
> Nowadays I use scanf("[^\n]") to flush input stream. Is this ok?
> 
> 
> 


Close. The correct call for scanf() to read up to a newline would be:



```
scanf("%*[^\n]");

```

This isn't complete though, because the newline will still be in the stream. You still need to extract it. One way would be to add an ignored character match in the format string:



```
scanf("%*[^\n]%*c");

```

This might not be as flexible though, if you want to do other stuff like count discarded characters. Another way would be to call getchar() after scanf():



```
scanf("%*[^\n]");
getchar();

```

Finally, there's the far more common (and faster!) method of calling getchar() in a loop. I'll even give you a function that you can call instead of having to write it ad hoc every time:



```
#include <stdio.h>

void clear_line(FILE *in)
{
    int ch;

    /* Do nothing if the stream is in an error state */
    if (ferror(in))
        return;

    /* Make sure any end-of-file  state is cleared */
    clearerr(in);

    /* Extract and discard up to a newline or end-of-file */
    do
        ch = getc(in);
    while (ch != '\n' && ch != EOF);

    /*
        Clear an end-of-file state if we stoped on EOF,
        but leave any legitimate error state for the caller
    */
    if (!ferror(in))
        clearerr(in);
}

```

Note that even this solution is incomplete when compared with what people expect from `fflush(stdin)` because it blocks for input when the stream is empty. fflush(), when flushing input streams is supported, will return without blocking on an empty stream. Unfortunately, there's no portable way to emulate that behavior, but it's fairly simple to just ensure that the stream is never empty when calling clear\_line().



> 
> fflush( std::cout );
> 
> 
> 


Yeah, that won't work. fflush() expects a pointer to FILE, not an std::ostream object. std::ostream has its own flush() member for synchronizing with the destination device.


It's also quite silly to flush cout explicitly prior to accepting input through cin because the two are tied together. As soon as you request input from cin, the tied stream (cout) is automatically flushed. Doing it yourself explicitly isn't harmful, but it *is* redundant.


It's actually a pretty nifty feature of the iostream library. Read more about it starting [here](http://www.cplusplus.com/reference/iostream/ios/tie/).
yes i already tried the uff input order like i mentioned in my post.

in the end i just added an additional layer in my neural network to swap from channel-first, to channel-last after the input has been read so this issue can be closed. how about this?

fn write\_some\_content(&mut self, hour: u32) -> result<(), std::io::error> {

if self.current\_hour != hour {

if let some(writer) = self.current\_file.as\_mut() {

writer.flush()?;

}

self.current\_file = none;

self.current\_hour = hour;

}

… it looks like you are right, this appears to be an issue. i will make sure that it gets fixed soon. when i was working and needed to clean the guys toilet i would, and otherwise i've sneaked in when the line has been too long not aware of any specific reason why it wouldn't work. whether it will hinder performance depends on what you're going to do. ok, in that case we believe it should be fixed in the upcoming 13.1 update, let us know if this is not the case once it’s out. because each time fill() is invoked it is only applying 0.047% opacity over what was previously drawn. draw() runs aprox 60 times per second … when u do well but not well enough.or when they hate the other team. hi @timh we are very sorry that the “shuffle my music” feature algorithm is not perfect yet. i can assure you that the feedback has been passed on to the product team several times. they had to focus on other features but i just asked for you again and will let you know as soon as have more info. in the meantime, i highly recommend that you simply shuffle your favourites, listen to your flow, listen to the new playlist top 2019  or shuffle your own playlists on other days for more variety. nvm, i think it might be because i was force running it. i reopened the script and it appears to work.