Ask Question. Asked 6 years, 4 months ago. Active 6 years, 4 months ago. Viewed 1k times. Improve this question. Gabriel Gabriel 1 1 gold badge 10 10 silver badges 26 26 bronze badges. Doing fflush stdin is illegal in standard C library. The behavior is undefined. AnT omg, I really didn't know that, can you explain more about this concept?
AnT: It does have undefined behavior, but it's not illegal. Still, using it here is probably not a good idea. You want to flush input in the sense of reading and discarding some data. See Using fflush stdin for a discussion of what it does. JonathanLeffler Thank you for the sugestion and the explanation.
Add a comment. Active Oldest Votes. Meanwhile the C11 standard under 7. Improve this answer. Arkku Arkku This is interesting!
So the propose of using fflush stdin into this program it is not the right thing to do, but it works pretty good to clean input buffer of the scanf, right? You haven' t given a very clear description of what sort of input might be entered by your input source. And, if you're dealing with humans typing input, you need to perform much better error handling.
IP Networking. Discussion at work, would a router work pluging a cable in wan1 and lan1? Shell Programming and Scripting. Hi, I has the following command in the script. This command works fine if I execute on command prompt. If I run the script, this is not working as expected deleting CR. Bonjour, Mon application en C sous linux tourne en redirigeant stdin vers un fichier.
PB: Je voudrais temporairement redonner la main au user sur le clavier. Alors je pensais ajouter system "appli2" ; dans appli1. Dans son main , appli2 fait seulement un When a process fork , the child share the same file descriptors as his father. Thus, they share the same stdin. I have tried this approach:. Script doesn't work, but commands inside work. Howdie everyone I have a shell script RemoveFiles. Files remained unremoved. I don't see any error message as it I need to gather at the command line the username, password and database sid.
I have the program currently doing this. The non-comment lines conform to the normal layout, so the root entry is:. The Linux behaviour corresponds to the documented POSIX behaviour, but the POSIX specification is far more careful in what it says — it specifies a file capable of seeking, but terminals, of course, do not support seeking. It is also much less useful than the Microsoft specification.
Microsoft documents the behaviour of fflush stdin. Apparently, it works as documented on the Windows platform, using the native Windows compiler and C runtime support libraries.
Despite documentation to the contrary, it does not work on Linux when the standard input is a terminal, but it seems to follow the POSIX specification which is far more carefully worded. According to the C standard, the behaviour of fflush stdin is undefined. POSIX adds the qualifier 'unless the input file is seekable', which a terminal is not. The behaviour is not the same as Microsoft's. Consequently, portable code does not use fflush stdin. Code that is tied to Microsoft's platform may use it and it will work, but beware the portability issues.
The POSIX standard way to discard unread information from a terminal file descriptor as opposed to a file stream like stdin is illustrated at How can I flush unread data from a tty input queue on a Unix system. According to the standard, fflush can only be used with output buffers, and obviously stdin isn't one. However, some standard C libraries provide the use of fflush stdin as an extension.
0コメント