2016-09-28 224 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