Can someone edit my code so that it prints the last N lines of a txt file? N will always be at least

Can someone edit my code so that it prints the last N lines of a txt file? N will always be at least 1. If N > than # of lines in file, then all is displayed. Here an example:

txtfile.txt:

This

is

a test.

1. ./check.out 2

     is

     a test.

int main(int argc, char* argv[])
{
   FILE* txtFile = NULL;
   int N;
   char* lines[100];
   int i;
   int numLines = 0;
   char x;

   txtFile = fopen(argv[1], “r”);
   N = atoi(argv[2]);

   while(!feof(txtFile))
   {
       fscanf(txtFile, “%x”, &x);
       if (x == ‘n’)
       {
           numLines++; //check
       }
   }

   printf(“lines: %dn”, (numLines -1));

   for (i = 0; i < (numLines -1); i++)
   {
       fgets(lines[i], 100, txtFile);
       //printf(“%s”, lines[i]);
   }
   for (i = 0; i< (numLines -1); i++)
   {
       printf(“%s”, lines[i]);
   }
   if (i = 0; N >= (numLines -1); i++)
   {
       printf(“”  
   }

   fclose(txtFile);
  
   return 0;
}

Need your ASSIGNMENT done? Use our paper writing service to score better and meet your deadline.


Click Here to Make an Order Click Here to Hire a Writer