Introduction
In this post, I am going to write a program to print the contents of a file in reverse order in c programming. What does this mean?
We will read a file from the end character by character. For writing this program, we need to perform the following steps.
Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle
- Open a file in reading mode.
- Move the file pointer to the end of the file using fseek() function.
- Count total number of bytes of the file using ftell() function.
- Read the file from the end character by character.
Now, before going further, let’s see the expected output first. This is the c program of file handling. Therefore, we need one file that we are going to open in the reading mode. Suppose that file is abc.txt and its contents are
Input File: abc.txt
abcd efgh ijkl mnop qrst uvwxyz
Also Read: C Program to Print Multiples of 5 using do-while loop
Now, we need to reverse the contents using our c program.
Expected Output

As you can see from the above expected output. These are the contents of the input file abc.txt in the reverse order.
Now, let us see the actual c program to reverse the contents of a file.
Print Contents of File in Reverse Order in C
#include<stdio.h> #include<stdlib.h> int main() { FILE *fp; int ft,i=0; fp=fopen("abc.txt","r"); if(fp==NULL) { printf("ERROR"); return 0; } fseek(fp,0,SEEK_END); ft=ftell(fp); while(i<ft) { i++; fseek(fp,-i,SEEK_END); printf("%c",fgetc(fp)); } fclose(fp); return 0; }
Explanation
Now, I am going to explain the important part of this program.
1. Open a file in read mode.
We have to display the contents of a file. So there is no need to write anything in the file. Therefore, we are going to open this file in the read mode using the statement
fp = fopen(" abc.txt ", "r");
Here, r means the read mode.
Also Read: C Program to Remove Zeros from a number
2. Move the file pointer to the end of the file using fseek() function.
For reading a file in the reverse order, we need to reach the end of the file. We can do this by using the fseek function. The fseek() function moves to a specific location. See the following code.
fseek(fp, 0, SEEK_END );
Here, fp means the file pointer which is associated with the input file abc.txt, 0 means don’t move anywhere, SEEK_END means move this file pointer to the end of the file.
3. Count total number of bytes of the file using ftell() function
We need to count the total number of bytes because we are going to use a while loop. Using this while loop, we will move the file pointer character by character. For this, we need one condition which terminates the loop. See the following code.
ft=ftell(fp); while(i<ft)
In the second step, we have moved the file pointer to the end of the file. But, we don’t know how many characters or bytes are there from beginning to end. The function ftell() will count the total bytes from beginning to the end and that value will be stored in the variable ft.
Also Read: The while loop in C Programming
In the while loop, we will check whether the value of i is less than ft or not. The initial value of i is 0. When the value of i becomes greater than ft then, that loop will be terminated.
4. Read the file from the end character by character.
In the while loop, I have written the following code.
i++; fseek(fp,-i,SEEK_END); printf("%c",fgetc(fp));
The while loop will repeat the above three lines. In the first line, we are incrementing the value of i.
In the second line, the fseek() function will move the file pointer by the ‘i’ character. The negative sign tells us that the file pointer will move in reverse order.
The third line will print that character which is pointed out by the value of i. For reading the character from the file, we are using the fgetc() function which reads the single character from the file.
I hope you have understood this program. If you know the Hindi language, then I have already made a video for this program. You can watch it here.
Thank you.
Some Important C Programs
- Program in C to Find Longest Line in a File
- Palindrome in C using Pointers
- Insert and Delete element in Array in C using switch case
- C Program to Add Alternate Elements of a 2D Array
- Arrays in C for Complete Beginners
- C Program to Find Area of a Circle using Preprocessor
- Program in C to Remove White Spaces and Comments from a File
- C Program to Print Numbers Except Multiples of n
- Reverse a Number using getchar and putchar function in c
- The while loop in C Programming