linux下的退格键小研究(补充)

来源:互联网 发布:配音秀软件 编辑:程序博客网 时间:2024/05/29 02:17

    本来以为之前的那个挨个读的程序没有问题了,结果代码刚插入到工程了就出现问题了,while循环直接略过,循环体根本没有执行,喵了个咪,这是个什么情况!

    排查了一下,发现是把上面一个输入流的\n读进来了,所以循环条件不成立,知道原因就好改了,代码如下:

#include <stdio.h>#define LEN 30void get_string(char * string, int len){char ch;int i=0;while((ch=getchar())=='\n');while(ch!='\n' && i<len-2 && i>=0 ){if(ch=='\b')string[--i]='\0';    else    string[i++]=ch;        ch=getchar();}string[i]='\0';}int main(){char string[LEN];get_string(string,LEN);printf("%s\n",string);return 0;}
    用一个while()来接收之前输入留下来\n,然后再读入,就像太祖说的,打扫干净屋子再请客!

阅读全文
0 0
原创粉丝点击