C Program to Read last n characters from a file

C Program to Read last n characters from a file

This page has a C Program to Read last n characters from a file. File may contain a set of records arranged in an order. The File may have a number of records containing same number of fields. All the records in the file may have a unique primary field (Columns). I mean that, the file is just like a table. Each file may contain a unique field called primary key. In addition, the files are accessible with the help of File Pointers. We can keep and use a file pointer to access records from the file. The below given C program is used to Read last n characters from a file. Kindly check out the program to Read last n characters from a file. The questions to Read last n characters from a file can be asked as,

1). C program to Read last n characters from a file
2). C Program to Read last n characters from bottom of a file

3). C Program to Read last n characters from the bottom of a file

4). C Program to read last n characters from the bottom of file


Read last n characters from a file 

  #include<stdio.h>
  int main(){
                FILE *f1;
                int n,total;
                char a;
                printf("\n Enter a number from which the Characters to be displayed : \n");
                scanf("%d", &n);
                f1 = fopen("FileData.txt", "r");
                if(f1==NULL)
                {
                puts("The file has no Information !!");
                }
                else
                {
                fseek(f1, 0, SEEK_END);
                total = ftell(f1);
                fseek(f1, (total - n), SEEK_SET);
                while(a != EOF){
                a = fgetc(f1);
                putchar(a);
                }
                }
                fclose(f1);
                getch();
   return (0);
   }


Sample Outputs:

At First, Create a Text file either through the Notepad or Ms-Word. If we do not create a file initially, the above program will produce the output as "The file has no Information !!". It is clear that, if you do not create a text file for using it in the program with 'r' mode, then the program will not have the possibility to read the number of characters from bottom of file. If you create a text file initially for using it in the program with 'r' mode, the program will read the number of characters from bottom of file as requested. After running the program, Verify the Contents of test1.txt file by opening it in Notepad or Ms-word !!


Related Popular Posts:

Other Related C Programs :


* C Program to find whether the given number is Odd or Even

* C Program to find the Sum of Odd Digits

* C Program to find the Sum of Even Digits

* C Program to find the Sum of Even and Odd Digits,

* C Program to Display Odd digits

* C Program to Display even digits

* C Program to Display Odd Numbers,

* C Program to Display Even Numbers,

* C Program to Check whether the given number is Armstrong number

* C Program to display the Armstrong numbers in Range

* C Program to display the Armstrong numbers between 1 and 1000

* C Program to check perfect number, perfect numbers in range

* C Program to Check Prime number, Prime numbers between 1 and 100

* C Program to display the Prime numbers in a Range

* C Program to display the Sum of Prime numbers

* C Program to Check Strong number

* C Program to display the Strong number in Range

* C Program to Check whether the given number is polindrome number or not

* C Program to Check polindrome number using for loop

* C Program to Check polindrome number using Recursion

* C Program to  check whether the given string is polindrome or not

* C Program to find the Roots of Quadratic equation

* C Program to display Fibonacci Series

* C Program to display the Fibonacci Series using an array

* C Program to display the Fibonacci Series using while loop

* C Program to find the Sum of Fibonacci Series, Factorial of Number

* C Program to find the Factorial without using Recursion

* C Program to find the Factorial using Pointers

* C Program to find the Factorial using Function

Bookmark and Share