c - Can fseek(stdin,1,SEEK_SET) or rewind(stdin) be used to flush the input buffer instead of non-portable fflush(stdin)? -


since discovered fflush(stdin) not portable way deal familiar problem of "newline lurking in input buffer",i have been using following when have to use scanf:

while((c = getchar()) != '\n' && c != eof); 

but today stumbled across line had noted cplusplus.com on fflush:

fflush()...in files open update (i.e., open both reading , writting), stream shall flushed after output operation before performing input operation. can done either repositioning (fseek, fsetpos, rewind) or calling explicitly fflush

in fact, have read before many times.so want confirm if can use of following before scanf() serve same purpose fflush(stdin) serves when supported:

fseek(stdin,1,seek_set); rewind(stdin); 

ps rewind(stdin) seems pretty safe , workable flush buffer, wrong?

mistake should have mentioned fseek(stdin,0,seek_set) if talking stdin can't use offset other 0 or 1 returned ftell() in case.

this portable idiom use:

while((c = getchar()) != '\n' && c != eof); 

several threads including this 1 explain why feesk won't work. same reasons doubt rewind work either, in fact man page says equivalent to:

(void) fseek(stream, 0l, seek_set) 

Comments

Popular posts from this blog

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -