使用getchar
(常見的,容易理解的)
int c;
while ((c = getchar()) != EOF && c != '\n'); /* <note the semicolon! */
if (c == EOF) {
if (feof(stdin)) {
/* Handle stdin EOF. */
}
else {
/* Handle stdin error. */
}
}
使用fgets
(不太常見,少可理解)
char buf[8];
while (fgets(buf, sizeof buf, stdin) != NULL) {
size_t len = strlen(buf);
/*
* Exit the loop if either EOF was encountered before '\n', or
* if '\n' is detected.
*/
if (len + 1 != sizeof(buf) || memchr(buf, '\n', len))
break;
}
if (feof(stdin)) {
/* Handle stdin EOF. */
}
else {
/* Handle stdin error. */
}
使用具有scanf
一個掃描集(可能是不常見的,易於理解)
/*
* Combining the scanset with assignment suppression (the '*' before the
* scanset) will return EOF on EOF/error and 0 if '\n' was read.
*/
if (scanf("%*[^\n]") == EOF) {
if (feof(stdin)) {
// Handle stdin EOF.
}
else {
// Handle stdin error.
}
}
getchar(); // Flush the '\n'.
使用getline
(可能是罕見的,困難)
char *buf = NULL;
size_t bufsize = 0;
ssize_t len;
/* getline() will stop reading on '\n' or EOF. */
len = getline(&buf, &bufsize, stdin);
/* No bytes read and EOF encountered, or there was an error. */
if (len == -1) {
if (feof(stdin)) {
/* Handle stdin EOF. */
}
else if (ferror(stdin)) {
/* Handle stdin error. */
}
else {
/* Handle errno error, if desired. */
}
/*
* The value of "buf" is indeterminate here, so you likely
* just want to return from the function/program at this point
* rather than continuing and potentially freeing an invalid
* buffer.
*/
}
free(buf);
當然,所有的這些方法都假定要處理的事情上EOF /錯誤發生不同於與\n
,甚至所有三個都是單獨的案例。例如,通過將上面的片斷之一到一個獨立的功能,你可以,如果\n
讀取或錯誤EOF
上EOF /錯誤,或\n
甚至0
,EOF
對EOF,並返回1
0
。
事情值得注意:
- 的
getchar
和fgets
方法是100%跨平臺的。我更喜歡getchar
方法的簡單性。
- 由於並非所有編譯器都實現掃描集(因此不符合C99標準),所以scanset方法不太適合跨平臺。
getline
方法也不是十分跨平臺的:它主要在GNU/Linux和其他一些POSIX操作系統上實現;這個時候不包括Windows。一旦你有一些管理內存和使用指針的經驗,寫一個自己並不難,但你最好使用前兩種方法之一,因爲編寫getline
的實現可能最終會使用fgetc
或fgets
無論如何(fgetc(stdin)
和getchar()
應該表現相同)。
閱讀C-faq問題[12.26a](http://www.c-faq.com/stdio/stdinflush.html)和[12.26b](http://www.c-faq.com) /stdio/stdinflush2.html)。 – haccks
你現有的'flushKeyBoard'看起來不錯,你到底有什麼問題? –
無論您想將「2q」視爲一個有效的int並放棄「q」,或者如果您想放棄整行,都必須在執行'scanf'的代碼片段中控制。如果你想考慮「2q」是一個錯誤(而不是讀「2」,然後丟棄q)你可以更新'flushKeyboard'來返回'bool'來表明它是否真的清除了某些東西。 –