First of all, thank you for your comment. It prompted me to review my article and correct some mistakes in the article. In particular, if two programs in the text are redirected to a file, an endless loop will occur. Now I have fixed this problem. Even if stdin is redirected to a file, there will be no endless loops. If there are other shortcomings in this article, please note that I am not grateful! Then, I disagree with some of my opinions on the upstairs. I would like to express some comments here. 1. According to the upstairs definition of errors, I say that fflush (stdin) is wrong. However, everyone has different understandings of errors. I think this is an error if a function can be implemented with standard code without or without it, but relies on Compiler/system-specific function implementation. Of course, this is just my opinion. Also, I think using Compiler/system-specific functions (such as fflush (stdin);) is not a bad style code. In my opinion, bad style refers to a lot of code stacked together, not seriously indented, lack of comments, unclear code layers, and the Division of functional modules. In addition, I am very impressed with the opinion that "fflush is not defined as the result of using fflush for input streams. It is a definition of fflush (stdin. I think this is a unique and novel insight. The brains on the upstairs are really good! I never thought about this. Sorry! 2. My solution does have problems. Thank you for pointing out. But the problem is not what you said, but redirection. If stdin is redirected to a file, my original program will indeed lead to an endless loop. The upstairs said, "when entering the end with Ctrl + Z or directly entering Ctrl + Z, the program will certainly enter an endless loop !", I tested it with TC, and entering Ctrl + z directly won't be an endless loop. However, After inputting some data, an endless loop does occur after entering Ctrl + Z. However, this is a TC problem! See the following code: # Include <stdio. h> Int main (void) { Int ch; While (getchar ()! = EOF ); If (feof (stdin )) {/* If the input stream is marked with an EOF tag */ Printf ("oh, no! EOF indicator is set now! /N "); } Clearerr (stdin);/* clear the EOF mark and error mark */ If (! Feof (stdin )) {/* If the input stream is not marked with an EOF tag */ Printf ("OK! EOF indicator is unset now! /N "); } If (getchar () = EOF) {/* If the data cannot be read from stdin */ Printf ("but why still cannot read from stdin? /N "); } Return 0; } Enter 21312313 ^ Z when compiling and running with TC. The result is as follows: 21312313 ^ Z Oh, no! EOF indicator is set now! OK! EOF indicator is unset now! But why still cannot read from stdin? Therefore, even if the EOF tag is not marked, data cannot be read from stdin if the input ends with ^ Z! This is a TC problem! The reason why my original program will have an endless loop when the input ends with ^ Z is that it cannot read data from stdin! Rewind (stdin) is used upstairs; then data can be read from stdin. It seems to be a specific TC function! But thanks to the upstairs, I found that if stdin is redirected to a file, my program will have an endless loop. However, I wrote these two programs only to demonstrate how to clear stdin. I didn't think too much about other factors. 3. I strongly disagree with the solution proposed above! The solution proposed above is better than using fflush (stdin). It uses compiler-specific functions. First, let's take a look at the standard definition of the rewind function: Void rewind (File * stream ); The rewind function sets the file location tag of the stream to a file. Start. If you do not consider it, it will clear the error mark of the stream, then the rewind Function Equivalent (Void) fseek (stream, 0l, seek_set );
The original article is as follows: The rewind function sets the file position indicator The stream pointed to by stream to the beginning of File. It is equivalent (Void) fseek (stream, 0l, seek_set) Could t that the error indicator for the stream is also Cleared. K & R's C programming language, Second Edition simply says Rewind (FP); equivalent to fseek (FP, 0l, seek_set); clearerr (FP ); It can be seen that the standard only means that rewind can set the file location mark of the stream to start the file, and clear the error mark of the stream, but does not define rewind (stdin) to clear stdin content, therefore, using rewind (stdin) may not clear stdin. In addition, if stdin is redirected to a file, using rewind will produce very "interesting" results. If you are interested, try it. |