C program to reverse the content of the file and print it Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Given a text file in a directory, the task is to print the file content backward i.e., the last line should get printed first, 2nd last line should be printed second, and so on.Examples: Input: file1.txt has: Welcome to GeeksforGeeks Output: GeeksforGeeks to WelcomeGeeksforGeeks Input: file1.txt has: This is line one This is line two This is line three This is line four This is line five Output: This is line five This is line four This is line three This is line two This is line one Approach: Initialise previous length of the text as 0.Find the length of the current line and add it to the previous length. This given the next starting index of the new line.Repeat the above steps till the end of the file.Initialise the array of length of the given message in the given file.Now rewind your file pointer and place the last pointer of the text to arr[K - 1] where K is the length of the array using fseek().Print the length of the last line and decrease K by 1 for printing the next last line of the file.Repeat the above steps until K is equals to 0. Below is the implementation of the above approach: C // C program for the above approach #include <stdio.h> #include <string.h> #define MAX 100 // Function to reverse the file content void reverseContent(char* x) { // Opening the path entered by user FILE* fp = fopen(x, "a+"); // If file is not found then return if (fp == NULL) { printf("Unable to open file\n"); return; } // To store the content char buf[100]; int a[MAX], s = 0, c = 0, l; // Explicitly inserting a newline // at the end, so that o/p doesn't // get effected. fprintf(fp, " \n"); rewind(fp); // Adding current length so far + // previous length of a line in // array such that we have starting // indices of upcoming lines while (!feof(fp)) { fgets(buf, sizeof(buf), fp); l = strlen(buf); a[c++] = s += l; } // Move the pointer back to 0th index rewind(fp); c -= 1; // Print the contents while (c >= 0) { fseek(fp, a[c - 1], 0); fgets(buf, sizeof(buf), fp); printf("%s", buf); c--; } return ; } // Driver Code int main() { // File name in the directory char x[] = "file1.txt"; // Function Call to reverse the // File Content reverseContent(x); return 0; } Input File: Output File: Comment More infoAdvertise with us Next Article C program to read a range of bytes from file and print it to console Y yashbeersingh42 Follow Improve Article Tags : C Programs Placements DSA Arrays File Handling +1 More Practice Tags : Arrays Similar Reads C Program to Print Contents of File C language allows users to process the files in its programs. Reading a file is a step-by-step process in which we first have to prepare the file only after which we can start reading. In this article, we will learn how to read and print the contents of a file using C program.The simplest method to 4 min read C program to read a range of bytes from file and print it to console Given a file F, the task is to write C program to print any range of bytes from the given file and print it to a console. Functions Used: fopen(): Creation of a new file. The file is opened with attributes as âaâ or âa+â or âwâ or âw++â.fgetc(): Reading the characters from the file.fclose(): For clo 2 min read C Program to Read Content of a File In C, reading a file is a step-by-step process in which we first have to prepare the file only after which we can start reading. It involves opening the file, reading its data, and closing the file.Opening the File: Opening the file loads the file into the memory and connect the file to the program 5 min read C Program to Read Content of a File In C, reading a file is a step-by-step process in which we first have to prepare the file only after which we can start reading. It involves opening the file, reading its data, and closing the file.Opening the File: Opening the file loads the file into the memory and connect the file to the program 5 min read C Program to Read Content of a File In C, reading a file is a step-by-step process in which we first have to prepare the file only after which we can start reading. It involves opening the file, reading its data, and closing the file.Opening the File: Opening the file loads the file into the memory and connect the file to the program 5 min read C program to append content of one text file to another Pre-requisite: File Handling in C Given the source and destination text files, the task is to append the content from source file to destination file and then display the content of the destination file.Examples: Input: file1.text This is line one in file1 Hello World. file2.text This is line one in 2 min read Like