程序不等待与scanf的用户输入("%C",&安培; YN); [英] Program doesn't wait for user input with scanf("%c",&yn);

查看:182
本文介绍了程序不等待与scanf的用户输入("%C",&安培; YN);的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是基本的code到一个程序我写使用文件C.我想检测输出文件是否已经存在,如果练它确实存在我想询问用户是否愿意覆盖与否。这是我第一次有同的fopen(outfilename,R)打开了outfilename文件的原因;而不是给fopen(outfilename,W);

据检测然而,文件的情况下不存在,如果它存在执行(输出文件已经存在,覆盖(Y / N))的printf的;语句,但完全忽略了scanf函数(%C,&安培; YN);声明!

在程序结束时的printf写着YN = 0如果文件不存在,只是YN =如果它确实存在。任何人可以帮助我吗?

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&FLOAT.H GT;
#包括LT&;&string.h中GT;诠释主要(无效){
    FILE * INF;
    FILE * OUTF;
    字符文件名[21],outfilename [21];
    焦炭YN ='0';    的printf(请输入输入文件名:);
    scanf函数(%S,&安培;文件名);    的printf(请输入一个输出文件名:);
    scanf函数(%S,&安培; outfilename);    / *读取*打开文件/
    INF = FOPEN(文件名,R);
    OUTF = FOPEN(outfilename,R);    / *检查输入文件是否存在* /
    如果(INF!= NULL){        / *检查输出文件不存在* /
        如果(OUTF == NULL){
            FCLOSE(OUTF);
            / *如果它不存在于写模式*通过打开文件/
            OUTF =的fopen(outfilename,W);
        }其他{
            / *如果文件确实存在,给覆盖或不是选项* /
            输出(输出文件已经存在,覆盖(Y / N):);
            scanf函数(%C,&安培; YN);
        }
    }
    的printf(\\ n YN =%C \\ N,YN);
    返回0;
}


解决方案

 的printf(请输入一个输出文件名:);
scanf函数(%S,&安培; outfilename);

当你进入第二个字符串,然后按Enter键,一个字符串的字符被放置在输入缓冲区中,他们分别是:输入的字符串和换行符character.The字符串获取由 scanf的但换行保持在输入缓冲器

另外,

  scanf函数(%C,&安培; YN);

您接下来的 scanf函数读取字符只是读取/消耗换行符,因此从不等待用户输入。

解决方案是消耗使用额外的换行符:

  scanf函数(%C,&安培; YN);
      ^^^< ------------注意空间

或使用的getchar()

您可能想看看我的答案 这里 作为一步解释详细的一步的问题。

This is the basic code to a program I am writing to practise using files in C. I am trying to detect whether the output file already exists and if it does exist I want to ask the user if they would like to overwrite it or not. This is the reason that I have first opened the outfilename file in with fopen(outfilename,"r"); as opposed to fopen(outfilename,"w");.

It detects the case of the file not existing, however, if it does exist it executes the printf("Output file already exists, overwrite (y/n):"); statement but completely ignores the scanf("%c",&yn); statement!

The printf at the end of the program reads "yn=0" if the file doesn't exist and just "yn=" if it does exist. Can anybody help me?

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

int main(void) {
    FILE *inf;
    FILE *outf;
    char filename[21],outfilename[21];
    char yn='0';

    printf("Please enter an input filename: ");
    scanf("%s",&filename);

    printf("Please enter an output filename: ");    
    scanf("%s",&outfilename);

    /* Open file for reading */
    inf=fopen (filename,"r");
    outf=fopen(outfilename,"r");

    /*check that input file exists*/
    if (inf!=NULL) {

        /*check that the output file doesn't already exist*/
        if (outf==NULL){
            fclose(outf);
            /*if it doesn't already exist create file by opening in "write" mode*/
            outf=fopen(outfilename,"w");
        } else {
            /*If the file does exist, give the option to overwrite or not*/
            printf("Output file already exists, overwrite (y/n):");
            scanf("%c",&yn);
        }
    }
    printf("\n yn=%c \n",yn);
    return 0;
}

解决方案

printf("Please enter an output filename: ");    
scanf("%s",&outfilename);

When you enter the second string and hit the ENTER key, a string and a character are placed in the input buffer, they are namely: the entered string and the newline character.The string gets consumed by the scanf but the newline remains in the input buffer.

Further,

scanf("%c",&yn);

Your next scanf for reading the character just reads/consumes the newline and hence never waits for user input.

Solution is to consume the extra newline by using:

scanf(" %c", &yn);
      ^^^   <------------Note the space

Or by using getchar()

You may want to check out my answer here for a detailed step by step explanation of the problem.

这篇关于程序不等待与scanf的用户输入(&QUOT;%C&QUOT;,&安培; YN);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆