C Program to Print the Content of a File in Reverse Order

In this article, we will learn how to create a program in C that prints the content of any given file (given by the user at run-time) in reverse order.

Things to Do Before the Program

Before creating the program that prints any file's content in reverse order, Let's first create a file with some content inside it. For example, let's write the following content in any text editor, say NotePad:

This is a sentence.
This is		placed inside the file
		named codescracker.txt
		in 
the current directory for C compiler

In the current directory, save the file with the name codescracker.txt. Here is the screenshot of the file along with its content and the folder where the file codescracker.txt is created and saved:

c program print file content in reverse order

Print File Content in Reverse Order

The question is: write a program in C that displays the content of a file in reverse order. The filename should be entered at run-time. The answer to this question is:

#include<stdio.h>
#include<conio.h>
int main()
{
    FILE *fp;
    char ch, fname[30], newch[500];
    int i=0, j, COUNT=0;
    printf("Enter the filename with extension: ");
    gets(fname);
    fp = fopen(fname, "r");
    if(!fp)
    {
        printf("Error in opening the file...\nExiting...");
        getch();
        return 0;
    }
    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");
    printf("The content in reverse order is:\n\n");
    for(j=(COUNT-1); j>=0; j--)
    {
        ch = newch[j];
        printf("%c", ch);
    }
    printf("\n");
    getch();
    return 0;
}

The above program was written in the Code::Blocks IDE; therefore, after a successful build and run, following is the sample run.

This program will ask the user to enter any filename along with its extension that is present inside the current directory (where the above source code is saved). We've already created a file called codescracker.txt in the current directory.Therefore, we can operate on that file for practical purposes to see how the output of the above program works:

print file content in reverse order c

Now supply the name of the file that was created earlier, say codescracker.txt, and press the ENTER key to see the content in its original order first and then the same content in reverse order at the end of the output, as shown here:

file content in reverse order c

Program Explained

Print the contents of the current file in reverse order

If you want to print the content of the current file (to which you are writing the current program, say codescracker.c), then we have to increase the size of the array, say newch[], because the code given above may be more than 500 characters. Therefore, increase the size and make it to 5000, which is newch[5000].

Note: Never forget to save the file before running it and enter the current file name to print its content in reverse order.

Let me create a program for you to check for the current file. This program will ask the user to enter the file name and extension and will print the file in reverse order. You can enter any filename with an extension. However, we will look for the current file here.

#include<stdio.h>
#include<conio.h>
int main()
{
    FILE *fp;
    char ch, fname[30], newch[5000];
    int i=0, j, COUNT=0;
    printf("Enter the filename with extension: ");
    gets(fname);
    fp = fopen(fname, "r");
    if(!fp)
    {
        getch();
        return 0;
    }
    ch = getc(fp);
    while(ch != EOF)
    {
        COUNT++;
        newch[i] = ch;
        i++;
        ch = getc(fp);
    }
    printf("\n");
    printf("The content in reverse order is:\n\n");
    for(j=(COUNT-1); j>=0; j--)
    {
        ch = newch[j];
        printf("%c", ch);
    }
    getch();
    return 0;
}

Here is the final snapshot of the sample run:

c print content of file in reverse order

C Quiz


« Previous Program Next Program »


Liked this post? Share it!