2016-09-28 61 views
0

當我在下面執行我的代碼時。它等待輸入文件名稱的輸入。但它不會等待我輸入文件名,而只是跳到它的_getch()部分。我無法添加一個句子。不工作控制檯不等待輸入。 (C語言)

代碼:

#include <stdio.h> 

main() { 

    FILE *fp; 
    char fnamer[100] = "";  //Storing File Path/Name of Image to Display 
    printf("\n\nPlease Enter the Full Path of the Image file you want to view: \n"); 
    scanf("%s", &fnamer); 
    fp = fopen(fnamer, "w"); 
    if (fp == NULL) 
    { 
     printf("\n%s\" File NOT FOUND!", fnamer); 
    } 
    char c[1000]; 
    printf("Enter a sentence:\n"); 
    gets(c); 
    fprintf(fp, "%s", c); 
    fclose(fp); 
    _getch(); 
} 

代碼工作,並等待進入一句話:

#include <stdio.h> 
#include <stdlib.h> /* For exit() function */ 
int main() 
{ 
    char c[1000]; 
    FILE *fptr; 
    fptr = fopen("program.txt", "w"); 
    if (fptr == NULL){ 
     printf("Error!"); 
     exit(1); 
    } 
    printf("Enter a sentence:\n"); 
    gets(c); 
    fprintf(fptr, "%s", c); 
    fclose(fptr); 
    return 0; 
} 

兩者是如此的相似正確的,在年底的提示,詢問了一句。這沒有意義。

+1

可能不相關,但刪除&符號:'scanf(「%s」,&fnamer);' - >'scanf(「%s 「,fnamer);' –

+2

[爲什麼要使用函數那麼危險 - 它不應該被使用](http://stackoverflow.com/questions/1694036/why-is這種功能非常危險 - 它不應該被使用) – LPs

+0

如果輸入與scanf格式字符串不匹配,則'scanf'使用起來會很複雜並且毫無用處。而是使用'fgets'。 –

回答

0

你必須在使用scanf後刷新你的輸入。

getchar()每scanf函數後

+1

@LPs我很抱歉。你錯了 –

+0

是的,對不起。我刪除了。 – LPs

+0

錯誤消息:錯誤錯誤LNK2019:無法解析的外部符號__getchar在函數_main中引用 – Ansh

0

您正在使用標準輸入接收輸入,這是你的第一個scanf函數電話後有哪些卡在晃來晃去\ n字符時遇到一個很常見的問題輸入鍵中的緩衝區。要清除一個便攜式簡單的方法這個緩衝區,添加類似

char c; 
while ((c = getchar()) != '\n' && c != EOF) { } 

這只是初始化字符,然後調用get焦炭作爲根據需要多次,直到達到「\ n」或「EOF」,這是你的情況。

TL;博士: 你的緩衝區看起來像這樣

hello.txt\n <-- "comes from the enter key" 

,當您嘗試使用get(三)所花費的\ n作爲下一個回車鍵。

+0

那麼,什麼是解決方案?添加while語句? – Ansh

+0

是的,在scanf之後。也考慮不使用gets(c),並使用fgets(c,1000,stdin);其中1000是您正在閱讀的字符串的大小。 –

0

規則是從不混合scanf和[f]得到scanf在下一個未使用的字符之前停止,通常爲空白,並且行尾由空白字符組成。

您可以嘗試在最後的scanf和第一個真正的fgets之間放置一個虛擬fgets。這將確保您在閱讀之前現在已位於行首。或者,您可以閱讀與fgets一致的所有內容,並使用sscanf解析這些行。我希望我的輸入是面向行的,這就是我所希望的。並始終控制輸入函數的返回值,它將避免一個程序突然發瘋,沒有任何指示,因爲一個輸入給出了一個被忽略的錯誤。

而在去年和並非最不重要的:從來沒有使用gets但只有fgets,前者是幾十年來在恥辱堂爲不可數緩衝的原因溢出

代碼將變成:

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

main() { 

    FILE *fp; 
    char fnamer[100] = "";  //Storing File Path/Name of Image to Display 
    char c[1000], *ix; 
    int cr; 
    printf("\n\nPlease Enter the Full Path of the Image file you want to view: \n"); 
    cr = scanf("%s", &fnamer); 
    if (cr != 1) { 
     // process error or abort with message 
    } 
    fp = fopen(fnamer, "w"); 
    if (fp == NULL) 
    { 
     printf("\n%s\" File NOT FOUND!", fnamer); 
     return 1; // do not proceed after a fatal error! 
    } 
    for(;;) { // read until a newline in input 
     ix = fgets(c, sizeof(c), stdin); 
     if (ix == NULL) { 
      // end of input: abort 
     } 
     if (strcspn(c, "\n") < strlen(c)) break; 
    } 
    printf("Enter a sentence:\n"); 
    ix = fgets(c, sizeof(c), stdin); 
    c[strcspn(c, "\n")] = '\0'; // remove end of line to get same data as gets 
    fprintf(fp, "%s", c); 
    fclose(fp); 
    _getch(); 
} 
0
main() { 

    FILE *fp; 
    char fnamer[100]="";  //Storing File Path/Name of Image to Display 
    printf("\n\nPlease Enter the Full Path of the Image file you want to view: \n"); 
    fgets (fnamer,100,stdin); //fgets(name, sizeof(name), stdin); 
    fp=fopen(fnamer,"w"); 
      if(fp==NULL) 
     { 
      printf("\n%s\" File NOT FOUND!",fnamer); 
      getch(); 
      exit(1); 
     } 

} 

我認爲最好的辦法,使用fgets insted scanf,因爲 fgets()可以讀取任何打開的文件,但scanf()只讀取標準輸入(用戶給定)。

fgets()從文件讀取一行文本;可能會使用scanf(),但也可以處理轉換 從字符串到內置數字類型

+0

@Ansh你試試這個嗎?它是否工作? – SMW