codescracker
c

C Program to Print Content of File in Reverse Order



« C Tutorial C Examples »

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

Things to Do before 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

Save the file with codescracker.txt as name, in the current directory. Here is the screenshot of 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. Filename should be entered at run-time. The answer to this question is:

// Print File Content in Reverse Order
// ----codescracker.com----

#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 under Code::Blocks IDE, therefore after successful build and run, here is the sample run:

This program will ask from user to enter any filename along with its extension that present inside the current directory (where the above source code is saved). Because we have already created a file named codescracker.txt, and placed it inside the current directory. Therefore we can operate on that file for practical purpose to see how the output of above program works:

print file content in reverse order c

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

file content in reverse order c

Program Explained

Print Current File's Content in Reverse Order

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

Note - Never fogot to save the file before run and enter the current file name to print its content in reverse order.

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

// Print Current File Content in Reverse Order
// ----codescracker.com---

#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 Tutorial C Examples »