Chinaunix首页 | 论坛 | 博客
  • 博客访问: 82537
  • 博文数量: 40
  • 博客积分: 1820
  • 博客等级: 上尉
  • 技术积分: 395
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-26 16:12
文章分类

全部博文(40)

文章存档

2011年(8)

2010年(32)

我的朋友

分类: C/C++

2010-05-07 22:18:52

/*
 * The program copies the input to output, and replaces tab by \t,
 * backspace by \b, backslash by \\
 *
 * zj
 * 2010-5-7
 */


#include <stdio.h>

int main(){
        int c;
        while((c = getchar()) != EOF){
                if(c == '\t')
                        printf("\\t");
                else if(c == 8)
                        printf("\\b");
                else if(c == '\\')
                        printf("\\\\");
                else
                        putchar(c);
        }
        return 0;
}

程序存在一个很严重的bug, 就是对退格键不好使。。。
查了几个资料,说的是:
The standard C library function getchar() buffers the input. That is, keystrokes are stored in a buffer, and are not made available to the user until "Enter" is pressed. Then they are fed to the user program, one at a time, as getchar() is called.
标准C库函数getchar()将输入存入缓存,就是说,所有的键盘输入都存入一个缓存,直到敲击“Enter”之前对用户而言都不可用。然后传给用户程序,当调用getchar()时,一次传一个字符。

The "good" thing about this is that you can backspace and make corrections any time before you press "Enter", and it's taken care of without your program having to handle such things. The "bad" thing is that if you want to act on any particular key as it is typed, you can't.
好处是你可以在敲击“Enter”之前进行退格和纠正,并且你自己程序不用处理这些操作。坏处是无法处理一些特殊字符。
阅读(546) | 评论(0) | 转发(0) |
0

上一篇:1_9

下一篇:1_11

给主人留下些什么吧!~~