免费注册 查看新帖 |

Chinaunix

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

看一段代码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-03-10 19:29 |只看该作者 |倒序浏览
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <errno.h>

  9. #define SIZE  1000001
  10. #define NLINE '\n'


  11. int main(int argc , char *argv[]){

  12.   char buf[SIZE];
  13.   char *p1,*p2,*p3,*p4;
  14.   struct stat  *fp;
  15.   int fd;
  16.   fp=(struct stat *)malloc(sizeof(struct stat));

  17.   if(argc != 2){
  18.        fprintf(stderr,"input error %s \n");
  19.        exit(1);
  20.   }

  21.   if( (fd=open(argv[1],O_RDONLY)) == -1 ){
  22.        fprintf(stderr,"open error %s \n",strerror(errno));
  23.        exit(1);
  24.   }
  25.   
  26.   if(fstat(fd,fp)== -1){
  27.        fprintf(stderr,"fstat error %s \n",strerror(errno));
  28.        exit(2);
  29.   }
  30.   
  31.   if(fp->st_size > (SIZE-1)){
  32.        fprintf(stderr,"buffer size is not big enough \n");
  33.        exit(3);
  34.   }

  35.   if(read(fd,buf,fp->st_size) == -1){
  36.        fprintf(stderr,"read error.\n");
  37.        exit(4);
  38.   }

  39.   p1=strchr(buf,NLINE);
  40.   p2=strrchr(buf,NLINE);  /*~~~~*/
  41.   *p2='\0';  /*~~~~~*/

  42.   do{
  43.   p2=strrchr(buf,NLINE);
  44.   p4=p2;
  45.   p3=p2+sizeof(char);
  46.   printf("%s\n",p3);
  47.   *p4='\0';
  48.   }while(p2 != p1);
  49.   
  50.   if(p2 == p1){
  51.     *p2 = '\0';
  52.     printf("%s\n",buf);
  53.   }

  54.   return 0;
  55. }
复制代码
它写的是个简单的tac命令。   波浪线注释的地方有点问题。它把最后一个回车符换为'\0'难道说我们编辑的文本结束时自动加了一个回车符号吗?

论坛徽章:
0
2 [报告]
发表于 2011-03-10 19:46 |只看该作者
这个是输入时保存的回车符吧

论坛徽章:
0
3 [报告]
发表于 2011-03-10 19:48 |只看该作者
回复 2# wb112200

是不是我们用文本编辑器写完最后一个字符一定是个回车?编辑器替我们添加的。

论坛徽章:
0
4 [报告]
发表于 2011-03-10 19:50 |只看该作者
回复 2# wb112200


    因为我在想啊,文本编辑的时候每一个都要换行敲个回车啊。最后一行写完直接存盘退出了也没用回车。  不知道存储的时候是不是有个'\n'
abncat 该用户已被删除
5 [报告]
发表于 2011-03-11 09:25 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP