In this tutorial you will learn about the C Program to Print Content of File in Reverse Order and its application with practical example.
C Program to Print Content of File in Reverse Order
In this tutorial, you will learn about the C Program to Print Content of File in Reverse Order with a practical example.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- Concepts of while loop.
- Conditional Statements in C programming.
- Using file functions of c language.
Print Content of File in Reverse Order
As we all know the file is a collection of characters, integers, and many data types. In strings, only one variable is declared which can store multiple values. First will take the string from the user. Then will pass that string to a for loop reading the file. The C programming language has many pre-defined functions for string manipulation. but in today’s tutorial, we will Print the Content of the File in Reverse Order.
With the help of this program, we can Print Content of File in Reverse Order
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declaring the variables for the program. 2. Taking the input file from the user. 3. Reading the file. 4. Counting the Data in the files. 5. Printing the results data in reverse order. 6. End program. |
Program:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
#include<stdio.h> int main() { //declaring the variable for the program FILE *fp; char ch, fname[30], newch[500]; int i=0, j, COUNT=0; //Taking the file input name with extension printf("Enter the filename with extension: "); gets(fname); //reading the data from the file taken in input fp = fopen(fname, "r"); if(!fp) { printf("Error in opening the file...\nExiting..."); getch(); return 0; } //printing the original content of the file printf("\nThe original content is:\n\n"); ch = getc(fp); while(ch != EOF) { COUNT++; putchar(ch); newch[i] = ch; i++; ch = getc(fp); } printf("\n\n\n"); //printing the content in reverse order printf("The content in reverse order is:\n\n"); for(j=(COUNT-1); j>=0; j--) { ch = newch[j]; printf("%c", ch); } printf("\n"); return 0; } |
Output:-
In the above program, we have first initialized the required variable.

- *fp = it will hold the address value.
- ch = it will hold the character value.
- fname[30] = it will hold the string value of the file name.
- newch[500] = it will hold the string value of the file.
- i = it will hold the integer value.
- j = it will hold the integer value.
- COUNT = it will hold the integer value.
Taking Input string from the user.

Reading the file.
Printing the original data from the file.

Printing output data.