免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1365 | 回复: 6
打印 上一主题 下一主题

我眼睛快花了,这两段代码有什么不同? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-05-26 23:35 |只看该作者 |倒序浏览
功能是:2.5秒内无标准输入则跳出timeout,否则屏幕回显输入字符.
下面的是书上的,可以用,fc上编译没问题:



  1. /*  Begin as usual with the includes and declarations
  2.     and then initialize inputs to handle input from the keyboard.  */

  3. #include <sys/types.h>
  4. #include <sys/time.h>
  5. #include <stdio.h>
  6. #include <fcntl.h>
  7. #include <sys/ioctl.h>
  8. #include <unistd.h>

  9. int main()
  10. {
  11.     char buffer[128];
  12.     int result, nread;

  13.     fd_set inputs, testfds;
  14.     struct timeval timeout;

  15.     FD_ZERO(&inputs);
  16.     FD_SET(0,&inputs);

  17. /*  Wait for input on stdin for a maximum of 2.5 seconds.  */

  18.     while(1) {
  19.         testfds = inputs;
  20.         timeout.tv_sec = 2;
  21.         timeout.tv_usec = 500000;

  22.         result = select(FD_SETSIZE, &testfds, (fd_set *)0, (fd_set *)0, &timeout);

  23. /*  After this time, we test result. If there has been no input, the program loops again.
  24.     If there has been an error, the program exits.  */

  25.         switch(result) {
  26.         case 0:
  27.             printf("timeout\n");
  28.             break;
  29.         case -1:
  30.             perror("select");
  31.             exit(1);

  32. /*  If, during the wait, we have some action on the file descriptor,
  33.     we read the input on stdin and echo it whenever an <end of line> character is received,
  34.     until that input is Ctrl-D.  */

  35.         default:
  36.             if(FD_ISSET(0,&testfds)) {
  37.                 ioctl(0,FIONREAD,&nread);
  38.                 if(nread == 0) {
  39.                     printf("keyboard done\n");
  40.                     exit(0);
  41.                 }
  42.                 nread = read(0,buffer,nread);
  43.                 buffer[nread] = 0;
  44.                 printf("read %d from keyboard: %s", nread, buffer);
  45.             }
  46.             break;
  47.         }
  48.     }
  49. }

复制代码

[ 本帖最后由 打靶归来 于 2006-5-26 23:38 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-05-26 23:38 |只看该作者

aa

下面这段是我自己写的,几乎和书上的一样,可以编译过去,
有TIMEOUT出现,但输入字符后,不能回显,timeout也不能输出了,
最后只能ctrl-c结束.

#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
int main()
{
        char buffer[128];
        int result,nread;
        fd_set inputs,testfds;
        struct timeval timeout;

        FD_ZERO(&inputs);
        FD_SET(0,&inputs);
        while(1) {
                testfds=inputs;
                timeout.tv_sec=1;
                timeout.tv_usec=50000;
                result=select(FD_SETSIZE,&testfds,(fd_set *)0,(fd_set *)0,
                                &timeout);
                switch(result) {
                        case 0:
                                printf("timeout\n");
                                break;
                        case -1:
                                perror("select");
                                exit(1);
                        defalut:
                                if(FD_ISSET(0,&testfds)) {
                                        ioctl(0,FIONREAD,&nread);
                                        if(nread==0){
                                                printf("keyboard down\n");
                                                exit(0);
                                        }       
                                        nread=read(0,buffer,nread);
                                        buffer[nread]=0;
                                printf("read %d char is %s",nread,buffer);
                                }
                        break;
                }
        }
}

[ 本帖最后由 打靶归来 于 2006-5-26 23:41 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2006-05-27 03:03 |只看该作者
defalut:

论坛徽章:
0
4 [报告]
发表于 2006-05-27 10:07 |只看该作者
哦,LZ把default:写成‘defalut:’这样就成了一个地址表识,其下面的code都属于case -1的情况!
有点晕倒!

论坛徽章:
0
5 [报告]
发表于 2006-05-27 10:15 |只看该作者
楼上的,可是用心去看了。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
6 [报告]
发表于 2006-05-27 13:32 |只看该作者
有个程序叫做 diff
另外,有很多软件都有比较文本的功能。

论坛徽章:
0
7 [报告]
发表于 2006-05-28 06:31 |只看该作者
谢谢各位,,看得我昏...忘了有个diff了...
改为default就可以了....
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP