tac命令的实现

来源:互联网 发布:蚂蚁代理浏览器软件 编辑:程序博客网 时间:2024/06/10 01:08

此程序实现简化的linux中的tac命令。即对文件按行倒序输出。

首先将文件指针置于文件尾,从后向前移动指针,

将两个换行符'\n'间的内容作为一行输出。


#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<sys/types.h>#include <sys/stat.h>#include <fcntl.h>#define BUFSIZE 4096char buf[BUFSIZE];void reverse(int fp); void main(int argc,char*argv[]){   int fp;  int i=1;  if(argc==1)  {   fp=STDIN_FILENO;   reverse(fp);  }  for(;i<argc;i++)  {    if((fp=open(argv[i],O_RDONLY))==-1)    {      fprintf(stderr,"open %s error\n",argv[i]);      continue;    }    reverse(fp);     close(fp);  }  exit(0);}void reverse(int fp){  off_t offset;  int count=0;  char tmp[2]={0};  if((offset=lseek(fp,-1,SEEK_END))==-1)//将文件指针指向最后一个字符。  {   printf("seek error\n");   return;  }   while(1)      //从后向前扫描文件,每遇到两个\n符,将之间的字符串作为一行输出。  {             //只包含后一个\n符(左开右闭),直至到文件开头。    pread(fp,tmp,1,offset);    count++;   //count用来统计两个\n符间字符个数。    while(strcmp(tmp,"\n")!=0&&offset!=0)    {     offset=lseek(fp,-1,SEEK_CUR);     pread(fp,tmp,1,offset);     count++;    }    if(offset==0) //若文件指针在文件开头,从此处读取长度count的字符串并输出。    {     pread(fp,buf,count,offset);     buf[count]='\0';     printf("%s",buf);     break;    }    else                  {                offset=lseek(fp,1,SEEK_CUR); //若文件指针指向\n符,指针向后移动一位,跳过该\n;      pread(fp,buf,--count,offset);//并读取长度count-1的字符串并输出。     buf[count]='\0';     printf("%s",buf);     offset=lseek(fp,-2,SEEK_CUR);//文件指针向前移动2位;     count=1;                     //重新计数,此时已经读取了一个\n。    }  }}

0 0