免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5274 | 回复: 5

read实现按行读取,计算一个文件的行数。 [复制链接]

论坛徽章:
0
发表于 2006-04-19 10:32 |显示全部楼层
昨天答应了“gooderfeng ”兄晚上回家写一个,但是回去后就跟朋友扯了会蛋,就没来得及写,今天早上匆忙之间就写了一个,方法比较笨,希望各位多提意见,谢谢!


  1. #include <unistd.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <errno.h>
  7. #include <string.h>

  8. #define BUFFER_SIZE 8192

  9. int main(int argc, char **argv)
  10. {
  11.         int  fd;
  12.         int  bytes_read, enter_end, count;
  13.         char buffer[2*BUFFER_SIZE], tmp_buf[BUFFER_SIZE+1];
  14.         char *token;
  15.         char *delim = "\n";
  16.         static int n=0;
  17.        
  18.         if (argc != 2) {
  19.                 fprintf(stderr, "Usage:%s filename\n\a", argv[0]);
  20.                 exit(1);
  21.         }

  22.         if ((fd = open(argv[1], O_RDONLY)) == -1) {
  23.                 fprintf(stderr, "Open %s Error:%s\n", argv[1], strerror(errno));
  24.                 exit(1);
  25.         }

  26.         memset(tmp_buf, 0x00, sizeof(tmp_buf));
  27.         memset(buffer, 0x00, sizeof(buffer));
  28.         while (bytes_read = read(fd, tmp_buf, BUFFER_SIZE)) {
  29.                 enter_end = 0;
  30.                 count     = 0;
  31.                 bytes_read = strlen(strcat(buffer, tmp_buf));
  32.                 if ((bytes_read == -1) && (errno != EINTR)) {
  33.                         perror("read data");
  34.                         break;
  35.                 } else if (bytes_read > 0) {
  36.                         if (buffer[bytes_read-1] == '\n')
  37.                                 enter_end = 1;
  38.                         token = strtok(buffer, delim);
  39.                         printf("%s\n", token);
  40.                         count += strlen(token) + 1;
  41.                         n++;
  42.                         for ( ; (token = strtok(NULL, delim)) != NULL; n++) {
  43.                                 count += strlen(token) + 1;
  44.                                 if (count >= bytes_read && enter_end != 1) {
  45.                                         strcpy(buffer, token);
  46.                                         n--;
  47.                                 } else
  48.                                         printf("%s\n", token);
  49.                         }
  50.                 }

  51.                 if (enter_end == 1)
  52.                         memset(buffer, 0x00, sizeof(buffer));
  53.                 memset(tmp_buf, 0x00, sizeof(tmp_buf));
  54.         }
  55.        
  56.         printf("Total lines: %d\n", n);
  57.         close(fd);
  58.        
  59.         return 0;
  60. }

复制代码

论坛徽章:
0
发表于 2006-04-19 10:49 |显示全部楼层
对一个文件统计回车换行的个数不行吗

论坛徽章:
0
发表于 2006-04-19 10:53 |显示全部楼层
原帖由 stiandao 于 2006-4-19 10:49 发表
对一个文件统计回车换行的个数不行吗

其主要目的是使文件按行输出,有的时候从网络接收一个文件,在接收的同时可能还需要对文件进行分析、分解。

论坛徽章:
0
发表于 2006-04-19 16:03 |显示全部楼层
必须是用 read吗?是为了在并发情况下的异步信号安全?

哦,现在才看到
其主要目的是使文件按行输出,有的时候从网络接收一个文件,在接收的同时可能还需要对文件进行分析、分解。

[ 本帖最后由 westgarden 于 2006-4-19 16:09 编辑 ]

论坛徽章:
0
发表于 2006-04-19 17:53 |显示全部楼层
谢谢,好好看看

论坛徽章:
0
发表于 2006-04-20 08:01 |显示全部楼层
无非就是自己做缓冲而已。fgets 的实现就是将缓冲放在了 FILE * 里,由标准库函数维护。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP