实现tail

编程之路刚刚开始,错误难免,希望大家能够指出。

自己实现一个tail的功能(使用IO系统调用),完全类似的操作步骤就不实现了,主要是让自己加深了解。

下面的代码不足之处很多,以后有空改正。

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <sys/stat.h>
  4 #include <fcntl.h>
  5 #include <unistd.h>
  6 
  7 #define BUF_SIZE 1024
  8 
  9 int GetFileLine(int fd)
 10 {
 11     int iLine = 0;
 12     char czBuf[BUF_SIZE+1] = {0};
 13     while(1)
 14     {
 15         memset(czBuf,0,BUF_SIZE+1);
 16         int ret = read(fd,czBuf,BUF_SIZE);
 17         if(ret < 0)
 18         {
 19             perror("read");
 20             return -1;
 21         }
 22         else if(ret == 0)
 23         {
 24             break;
 25         }
 26         else
 27         {
 28             for(int index = 0;index < BUF_SIZE;index++)
 29             {
 30                 if(czBuf[index] == '
')
 31                 {
 32                     iLine++;
 33                 }
 34             }
 35         }
 36     }
 37 
 38     int ret = lseek(fd,0,SEEK_SET);
 39     if(ret < 0)
 40     {
 41         perror("lseek");
 42         return -2;
 43     }
 44 
 45     return iLine;
 46 }
 47 
 48 int myTail(int fd,int line)
 49 {
 50     int iLine = GetFileLine(fd);
 51     int ret = -1;
 52     char czBuf[BUF_SIZE+1] = {0};
 53     if(iLine < 0)
 54     {
 55         return -1;
 56     }
 57 
 58     if(line < iLine)
 59     {
 60         int iStartLine = iLine - line - 1;
 61         int iCurLine = 0;
 62         int flag = -1;
 63         while(1)
 64         {
 65             memset(czBuf,0,BUF_SIZE+1);
 66             ret = read(fd,czBuf,BUF_SIZE);
 67             if(ret < 0)
 68             {
 69                 perror("read");
 70                 return -1;
 71             }
 72             else if(ret == 0)
 73             {
 74                 break;
 75             }
 76             else
 77             {
 78                 if(flag == -1)
 79                 {
 80                     for(int index = 0;index < BUF_SIZE;index++)
 81                     {
 82                         if(czBuf[index] == '
')
 83                         {
 84                             if(iCurLine == iStartLine)
 85                             {
 86                                 flag = 0;
 87                                 printf("%s",czBuf+index);
 88                                 break;
 89                             }
 90                             iCurLine++;
 91                         }
 92                     }
 93                 }
 94                 else if(flag == 0)
 95                 {
 96                     printf("%s",czBuf);
 97                 }
 98             }
 99         }
100     }
101     else
102     {
103         while(1)
104         {
105             char czBuf[BUF_SIZE] = {0};
106             ret = read(fd,czBuf,BUF_SIZE);
107             if(ret < 0)
108             {
109                 perror("read");
110                 return -3;
111             }
112             else if(ret == 0)
113             {
114                 break;
115             }
116             else
117             {
118                 printf("%s",czBuf);
119             }
120         }
121     }
122     return 0;
123 }
124 
125 int main(int argc,char *argv[])
126 {
127     int fd = open("test.txt",O_RDONLY);
128     if(fd < 0)
129     {
130         perror("open");
131         return -1;
132     }
133 
134     int ret = myTail(fd,3);
135     if(ret < 0)
136     {
137         close(fd);
138         return -2;
139     }
140 
141     close(fd);
142     return 0;
143 }
原文地址:https://www.cnblogs.com/jiangyibo/p/8858582.html