⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tail.c

📁 语法分析器 完成从c/c++向C++转变
💻 C
字号:
/*  Tail.C     John Tal      Based on Unix tail command but not fully implemented here */


#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

struct tails{
	   char *inptr; /*  data read from file  */
	   int  next;   /*  which tail[] member is next, numeric array index */
        };


main(argc,argv)
int argc;
char **argv;
{
     int   lines,i,n,head,oldi,pass;
     FILE  *fp;
     struct tails tail[25];
     char  buffer[255];


     printf("tail, pc-dos version by john tal, 09/1988\n");
     if(argc == 2)
      	lines = 20;
     else{
      	printf("usage:  tail filename");
      	return(1);
     }


     for(i = 0; i < lines; i++)
        tail[i].inptr = malloc(255);

     i = 0;
     head = 0;   /*  initial head of circular buffer */
     n = 255;    /*  max bytes to read into each buffer  */
     pass = 1;   /*  passes through internal buffers */
     if((fp = fopen(argv[1],"r")) != NULL){
        while(fgets(buffer,n,fp) != NULL){
           oldi = i;   /*  remember */
           i++;        /*  next slot */

           if(i == lines){
              i = 0;      /*  reset to beginning of buffers */
              pass++;     /*  another pass */
           }
           if(pass != 1)
              head = tail[i].next;   /* new head */

           tail[oldi].next = i;   /*  connect last to this one */
           tail[i].next = -1;     /*  this one is new tail */
           strcpy(tail[i].inptr,buffer);  /*  get data */
        }
        /* head = i; */  /* final head */
        fclose(fp);
        i = head;
        while(i != -1){
	   printf("%s",tail[i].inptr);
	   i = tail[i].next;
        }

        /*  free-up malloc'd stuff */
        for(i = 0; i < lines; i++)
           free(tail[i].inptr);

     }
     else{
         printf("error: could not open file %s\n",argv[1]);
     }


}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -