getchar并不使用scanf函数时,不能停止 [英] getchar does not stop when using scanf

查看:279
本文介绍了getchar并不使用scanf函数时,不能停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很难理解的getchar()。在下面的程序的getchar 按预期工作:

 的#include<&stdio.h中GT;
诠释的main()
{
    的printf(请输入型继续......);
    的getchar();
    返回0;
}

然而,在下面的程序,的getchar 不产生延迟,程序结束:

 的#include<&stdio.h中GT;诠释的main()
{
    字符命令[100];
    scanf函数(%S,命令);
    的printf(请输入型继续......);
    的getchar();
    返回0;
}

我有以下weired解决方法,它的工作原理,但我不明白为什么:

 的#include<&stdio.h中GT;诠释的main()
{
    字符命令[100];
    INT I;
    scanf函数(%S,命令);
    的printf(请输入型继续......);
    而(的getchar()!='\\ n'){
      I = 0;
    }
    的getchar();
    返回0;
}

所以我的问题是:结果
  1.什么是 scanf函数在做什么?为什么 scanf函数做到这一点?结果
  2.为什么我的工作围绕工作吗?结果
  3.什么是效仿下面的Python code的好方法:

 的raw_input(类型回车键继续)


解决方案

您键入一个新行后,输入只发送到该程序,但

  scanf函数(%S,命令);

离开输入缓冲区的新行,因为%S (1)当第一个空白字符一些非后遇到格式停止-whitespace,的getchar()然后返回换行符立即并不需要等待进一步的输入。

您变通办法,因为它之前调用将清除输入缓冲区的新行的getchar()一次。

要模仿的行为,在打印消息前清空输入缓冲区,

  scanf函数(%S,命令);
INT℃;
做{
    C =的getchar();
}而(C ='\\ n'和;!和C = EOF!);
如果(C == EOF){
    //输入流结束,做点什​​么,也许退出
}其他{
    的printf(请输入型继续\\ n);
    的getchar();
}

(1)注意使用%S scanf函数是非常不安全的,你应该限制输入到你的缓冲区可与现场的宽度, scanf函数(%99秒,命令)将读取最多99(的sizeof(命令) - 1))的字符输入命令,留下的空间为0,终止

I have a difficulty understanding getchar(). In the following program getchar works as expected:

#include <stdio.h>


int main()
{
    printf("Type Enter to continue...");
    getchar();
    return 0; 
} 

However, in the following program, getchar does not create a delay and the program ends:

#include <stdio.h>

int main()
{
    char command[100];
    scanf("%s", command );
    printf("Type Enter to continue...");
    getchar();
    return 0; 
} 

I have the following weired workaround, which works, but I don't understand why:

#include <stdio.h>

int main()
{
    char command[100];
    int i;
    scanf("%s", command );
    printf("Type Enter to continue...");
    while ( getchar() != '\n') {
      i=0; 
    }
    getchar();
    return 0;    
}

So my questions are:
1. What is scanf doing? Why does scanf do this ?
2. Why is my work around working?
3. What is a good way to emulate the following Python code:

raw_input("Type Enter to continue")

解决方案

The input is only sent to the program after you typed a newline, but

scanf("%s", command );

leaves the newline in the input buffer, since the %s(1) format stops when the first whitespace character is encountered after some non-whitespace, getchar() then returns that newline immediately and doesn't need to wait for further input.

Your workaround works because it clears the newline from the input buffer before calling getchar() once more.

To emulate the behaviour, clear the input buffer before printing the message,

scanf("%s", command);
int c;
do {
    c = getchar();
}while(c != '\n' && c != EOF);
if (c == EOF) {
    // input stream ended, do something about it, exit perhaps
} else {
    printf("Type Enter to continue\n");
    getchar();
}

(1) Note that using %s in scanf is very unsafe, you should restrict the input to what your buffer can hold with a field-width, scanf("%99s", command) will read at most 99 (sizeof(command) - 1)) characters into command, leaving space for the 0-terminator.

这篇关于getchar并不使用scanf函数时,不能停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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