I implemented below code but i want tailbuf[] should be in order. I want tailbuf[9] should be last line, tailbuf[8] second and so on. Please help me how i cant implement that in this code.
#include<stdio.h>
#include<stdlib.h>
char ** lastnumlines(FILE *fp, unsigned int num)
{
int count = num;
int n, i, iNo1 = 0, iNo2= 0,z = 0;
size_t MAXSIZE = 1024;
char **tailbuf=calloc(count, sizeof(char *));
for (i = 0; i < count; i++)
{
tailbuf[i] = calloc(MAXSIZE, sizeof(char));
}
while (getline(&tailbuf[iNo2], &MAXSIZE, fp) != EOF)
{
iNo2 = (iNo2 + 1) % count;
if (iNo2 == iNo1)
{
iNo1 = (iNo1 + 1) % count;
}
}
i = iNo2;
int k = 0;
do{
printf("%s\n",tailbuf[i]);
i = (i+1) % count;
}while (i != iNo2);
free(tailbuf);
}
int main()
{
char *filename = "demo1.txt";
FILE *fp = fopen(filename,"r");
if(fp == NULL)
{
printf("Unable to open file!\n");
exit(1);
}
lastnumlines(fp,10);
fclose(fp);
return 0;
}
How i can tailbuf in order?