免费注册 查看新帖 |

Chinaunix

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

[C] UNPv1 程序清单3_18 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-15 13:03 |只看该作者 |倒序浏览
是关于采用自定义的缓冲区实现“预读”处理的例子。
运行的时候得不到结果,帮我看看吧,谢谢!

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. extern int      errno;

  9. static int      read_cnt;
  10. static char     read_buff[20];
  11. static char     *read_ptr = NULL;

  12. static my_read(int fd, char *ptr)
  13. {
  14. printf("444444444444444444444\n");
  15.         if(read_cnt <= 0){
  16. printf("555555555555555555555\n");
  17.                 again:
  18.                 if((read_cnt = read(fd, read_buff, sizeof(read_buff)) < 0)){
  19.                         if(EINTR == errno){
  20.                                 goto again;
  21.                         }
  22.                         return -1;
  23.                 }
  24.                 else if(0 == read_cnt){
  25.                         return 0;
  26.                 }
  27.                 read_ptr = read_buff;
  28. printf("read_cnt = %d\n", read_cnt);
  29.         }
  30.         read_cnt--;
  31.         *ptr = *read_ptr++;
  32.         return 1;
  33. }

  34. ssize_t readline(int fd, void *vptr, size_t maxlen)
  35. {
  36.         char            *ptr = (char *)vptr;
  37.         char            i, c, n;
  38. printf("1111111111111111111111111111\n");
  39.         for(i = 1; i <= maxlen; i++){
  40. printf("2222222222222222222222222222\n");
  41.                 if((n = my_read(fd, &c)) == 1){
  42. printf("3333333333333333333333333333\n");
  43.                         *ptr++ = c;
  44.                         if('\n' == c){
  45.                                 break;
  46.                         }
  47.                 }
  48.                 else if(0 == n){
  49.                         *ptr = 0;
  50.                         return (i-1);
  51.                 }
  52.                 else{
  53.                         return -1;
  54.                 }
  55.         }
  56.         *ptr = 0;
  57. printf("i = %d\n", i);
  58.         return i;
  59. }

  60. ssize_t readlinebuf(void **pptr)
  61. {
  62.         if(read_cnt){
  63.                 *pptr = read_ptr;
  64.         }
  65.         return read_cnt;
  66. }

  67. int main(int argc, char **argv)
  68. {
  69.         int             nread, nwrite;
  70.         int             nleft;
  71.         char            *pptr = NULL;

  72.         nread = readline(STDIN_FILENO, read_buff, 10);
  73.         printf("nread = %d\n", nread);
  74.         nleft = readlinebuf((void *)&pptr);
  75.         printf("nleft = %d\n");
  76.         printf("%s", pptr);

  77.         return 0;
  78. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2008-07-15 15:41 |只看该作者
代码写成这样,佩服佩服

论坛徽章:
0
3 [报告]
发表于 2008-07-15 21:26 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
4 [报告]
发表于 2008-07-15 22:50 |只看该作者
原帖由 xiaozhu2007 于 2008-7-15 13:03 发表
是关于采用自定义的缓冲区实现“预读”处理的例子。
运行的时候得不到结果,帮我看看吧,谢谢!

#include
#include
#include
#include
#include
#include
#include
extern int      errno;
...



LZ使用GDB调试一下先,定位一下错误啊。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP