/*模拟linux下tail -n命令*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#define RUFSIZE 16
int main(int argc,char *argv[])
{
int file;
int line;
int i,count=0,buf_len;
int flag=0,flag2=1;
char ch='A',rbuf[RUFSIZE];
char buffer[4096];
ssize_t tread;
if(argc==3)
{
line=-atoi(argv[1]);
if(line<0)
exit(1);//如果输入的不是-n,则使用exit函数退出
if((file=open(argv[2],O_RDONLY))<0)
perror("Open error!\n");
flag=lseek(file,-RUFSIZE,SEEK_END);
if(flag==-1)
{
lseek(file,0,SEEK_SET);
flag2=0;
}
else
{
read(file,rbuf,RUFSIZE);
buf_len=sizeof(rbuf);
}
while(flag>0 && flag2)
{
for(i=0;i<buf_len;i++)
{
if(rbuf[i]=='\n')
count++;
if(count>line)
break;
}
if(count>
模拟linux下tail -n命令
最新推荐文章于 2024-06-02 10:34:22 发布