2010-03-02 75 views
9

我對C非常陌生,並且在將數據輸入到程序時遇到了問題。獲取前在C. Scanf中輸入。問題

我的代碼:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

int main(void) { 
    int a; 
    char b[20]; 

    printf("Input your ID: "); 
    scanf("%d", &a); 

    printf("Input your name: "); 
    gets(b); 

    printf("---------"); 

    printf("Name: %s", b); 

    system("pause"); 
    return 0; 
} 

它允許輸入ID,但它只是跳過輸入的其餘部分。如果我改變這樣的命令:

printf("Input your name: "); 
    gets(b); 

    printf("Input your ID: "); 
    scanf("%d", &a); 

它會工作。雖然,我不能改變順序,我需要它就像現在一樣。有人能幫我嗎 ?也許我需要使用其他一些功能。謝謝!

+1

得到(3): 「的獲得()函數不能安全地使用,因爲它缺乏 邊界檢查,而不能用於調用程序。可靠的 確定下一條輸入線的長度,使用此功能 可以使惡意用戶通過緩衝區溢出攻擊任意改變正在運行的程序的功能。強烈建議 fgets()函數用於所有c ASES。 (請參閱FSA。)「 不要使用它 – 2010-03-02 20:39:01

+3

更簡單:如果你使用'gets',飛行的狂暴攻擊豹貓會撕開你的眼窩,所以不要 – 2010-03-02 20:45:08

+2

'scanf'是**邪惡* * - http://c-faq.com/stdio/scanfprobs.html – jschmier 2010-03-02 22:07:05

回答

10

嘗試:

scanf("%d\n", &a); 

僅獲取讀取 '\ n' 是在scanf葉此外,你應該用fgets沒有得到:http://www.cplusplus.com/reference/clibrary/cstdio/fgets/以避免可能的緩衝區溢出。

編輯:

如果上述方法無效,請嘗試:

... 
scanf("%d", &a); 
getc(stdin); 
... 
+0

嗯。我試過scanf(「%d \ n」, &a);但它似乎並沒有工作,我輸入ID後,我只是沒有看到程序做任何其他事情。 – Dmitri 2010-03-02 20:37:19

+0

哇!謝謝!這工作得很好!感謝您對fgets的建議。一定會使用它!!謝謝! – Dmitri 2010-03-02 20:47:46

7

scanf不消耗換行,並因而fgets天敵。不要把它們放在一起,沒有一個好的黑客。這兩個選項都可以工作:

// Option 1 - eat the newline 
scanf("%d", &a); 
getchar(); // reads the newline character 

// Option 2 - use fgets, then scan what was read 
char tmp[50]; 
fgets(tmp, 50, stdin); 
sscanf(tmp, "%d", &a); 
// note that you might have read too many characters at this point and 
// must interprete them, too 
+0

太棒了!!非常感謝! – Dmitri 2010-03-02 20:51:05

+0

另外,使用'scanf(「%d%* c」,&a)'應該可以工作。 '%* c'項導致'scanf'讀入一個字符(換行符),但星號會導致該值被丟棄。這將使'scanf'吃掉換行符而不需要單獨的函數調用。 – bta 2010-03-02 20:57:14

+0

這兩個選項都好像它們具有相同的基礎一樣,但'scanf'很難正確使用,所以避免完全使用它(請參閱comp.lang.c FAQ鏈接)。 – jamesdlin 2010-03-03 03:23:41

1
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

int main(void) { 
     int a; 
     char b[20]; 
     printf("Input your ID: "); 
     scanf("%d", &a); 
     getchar(); 
     printf("Input your name: "); 
     gets(b); 
     printf("---------"); 
     printf("Name: %s", b); 
     return 0; 
} 



Note: 
    If you use the scanf first and the fgets second, it will give problem only. It will not read the second character for the gets function. 

    If you press enter, after give the input for scanf, that enter character will be consider as a input f or fgets. 
2

scanf函數不會消耗\ n因此它將採取得到下面的scanf函數。像這樣在scanf之後刷新輸入流。

#include <stdlib.h> 
#include <string.h> 

int main(void) { 
    int a; 
    char b[20]; 

    printf("Input your ID: "); 
    scanf("%d", &a); 
    fflush(stdin); 
    printf("Input your name: "); 
    gets(b); 

    printf("---------"); 

    printf("Name: %s", b); 

    system("pause"); 
    return 0; 
} 
+2

在某些C庫中,fflush(stdin)和fflush(NULL)都會刷新stdout和stderr,但這是完全不可移植的! – 2010-11-29 22:08:13

0

你應該這樣做。

fgetc(stdin); 
    scanf("%c",&c); 
    if(c!='y') 
    { 
     break; 
    } 
    fgetc(stdin); 

在閱讀完讀取後從scanf讀取輸入。

0

只需使用2得到()函數

當你想使用gets()一個scanf()函數後,你要確保你使用的得2()函數,並針對上述案例編寫代碼如:

int main(void) { 
    int a; 
    char b[20]; 

    printf("Input your ID: "); 
    scanf("%d", &a); 

//the change is here********************* 
    printf("Input your name: "); 
    gets(b); 
    gets(b); 
//the change is here********************* 

    printf("---------"); 

    printf("Name: %s", b); 

    system("pause"); 
    return 0; 
} 

有關說明([email protected]);

+0

歡迎來到SO!請記住,英語不是所有人的第一語言,因此請嘗試發佈語法正確的答案,而不是將'u'用於'您' - 這不適用於Google翻譯! – 2016-01-09 09:59:28

0

scanf("%d", &a);無法讀取返回值,因爲%d只接受十進制整數。因此,在下一個scanf的開頭添加一個\n以忽略緩衝區內的最後一個\n

然後,scanf("\n%s", b);現在可以讀取字符串沒有問題,但scanf停止閱讀時發現一個空格。因此,將%s更改爲%[^\n]。它的意思是:「讀寄託都不過\n

scanf("\n%[^\n]", b);

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

int main(void) { 
    int a; 
    char b[20]; 

    printf("Input your ID: "); 
    scanf("%d", &a); 

    printf("Input your name: "); 
    scanf("\n%[^\n]", b); 
    //first \n says to ignore last 'return' 
    //%[^\n] read until find a 'return' 
    printf("---------\n"); 
    printf("Name: %s\n\n", b); 

    system("pause"); 
    return 0; 
} 
+1

請加說明。 – coatless 2016-07-05 17:35:47

+0

解釋添加@Coatless – 2016-07-05 18:02:01

0

scanf函數試圖解析字符以外的東西之前,會自動刪除空白。 %c%n,%[]是不會刪除前導空格的異常。

gets正在讀取之前scanf左側的換行符。抓住用換行符getchar();

scanf("%d", &a); 
getchar(); // catches the newline character omitted by scanf("%d") 
gets(b); 

https://wpollock.com/CPlus/PrintfRef.htm