Q:

C program to read last n characters of text file

0

Write a C program to read last n characters of text file. Here’s simple program to read last n characters of text file in C Programming Language.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Below is the source code for C program to read last n characters of text file which is successfully compiled and run on Windows System to produce desired output as shown below :

 
 


SOURCE CODE : :

/*  C program to read last n characters of text file  */

/* -----------------file4.txt contains text-------------

  Perpetual sincerity out suspected necessary
  one but provision satisfied. Respect nothing
  use set waiting pursuit nay you looking.
  If on prevailed concluded ye abilities.
  Address say you new but minuter greater.
  Do denied agreed in innate. Can and
  middletons thoroughly themselves him.
  Tolerably sportsmen belonging in september
  no am immediate newspaper. Theirs expect
  dinner it pretty indeed having no of.
  Principle september she conveying did
  eat may extensive.

--------------------------------------------------------- */

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

int main() {

   FILE *fp;
   char ch;
   int num;
   long length;

   printf("Enter the value of n : ");
   scanf("%d", &num);

   fp = fopen("C:\\Users\\acer\\Documents\\file4.txt", "r");
   if (fp == NULL) {
      puts("\ncannot open this file");
      exit(1);
   }

   fseek(fp, 0, SEEK_END);
   length = ftell(fp);
   fseek(fp, (length - num), SEEK_SET);

   do {
      ch = fgetc(fp);
      putchar(ch);
   } while (ch != EOF);

   fclose(fp);
   return(0);
}

OUTPUT : :


/*  C program to read last n characters of text file  */

Enter the value of n : 60

of. Principle september she conveying did eat may extensive.

Above is the source code for C program to read last n characters of text file which is successfully compiled and run on Windows System.The Output of the program is shown above .

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a C Program Delete Line from text File using... >>
<< C program to compare data of two files using File ...