tac命令的实现 分类: linux 2014-06-02 00:08 344人阅读 评论(0) 收藏

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

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

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


#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define BUFSIZE 4096
char 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
",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
");
   return;
  } 
  while(1)      //从后向前扫描文件,每遇到两个
符,将之间的字符串作为一行输出。
  {             //只包含后一个
符(左开右闭),直至到文件开头。
    pread(fp,tmp,1,offset);
    count++;   //count用来统计两个
符间字符个数。
    while(strcmp(tmp,"
")!=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]='';
     printf("%s",buf);
     break;
    }
    else              
    {           
     offset=lseek(fp,1,SEEK_CUR); //若文件指针指向
符,指针向后移动一位,跳过该
; 
     pread(fp,buf,--count,offset);//并读取长度count-1的字符串并输出。
     buf[count]='';
     printf("%s",buf);
     offset=lseek(fp,-2,SEEK_CUR);//文件指针向前移动2位;
     count=1;                     //重新计数,此时已经读取了一个
。
    }
  }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/luo-peng/p/4646261.html