使用getchar函数字符串输入() [英] String input using getchar()

查看:489
本文介绍了使用getchar函数字符串输入()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code使用的getchar()接受输入行。

The following code uses getchar() to accept a line of input.

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

int main()
{
 char *rawString = (char *)malloc(200*sizeof(char));
 char *rawStringInitial = rawString;
 char c;
 c=getchar();
 while(c!='\n')
 {
  *rawString=c;
  rawString++;
  c=getchar();
 }
 *rawString='\0';
 printf("\n[%s]\n",rawStringInitial);
 return(0);
}

打字时,如果我preSS退格,应该不是也被接受的getchar()及存储在rawString尖的位置?然而,简单地输出没有显示任何特殊字符,最后一个字符串。可能有人解释为什么?

While typing, if I press backspace, shouldn't it also be received by getchar() & stored in the rawString-pointed location? However the output simply shows the final string without any special characters. Could someone explain why?

推荐答案

标准输入(通常)缓冲的;像退格非打印字符由终端服务器处理,库函数如的getchar()将永远不会看到它们。

Standard input is (usually) buffered; non-printing characters like backspace are handled by the terminal server, and library functions like getchar() will never see them.

如果你需要读取原始键击,那么你就需要使用一些C标准库之外。

If you need to read raw keystrokes, then you will need to use something outside of the C standard library.

这篇关于使用getchar函数字符串输入()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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