Reverse the content of a file and store it in another

The article explains how to store the reverse of the first text file’s data to a second text file. In this post, it has been assumed that there is no text in the first text file. So we will write some text in a first text file by passing data to reverse function and then in reverse function we will copy the reverse of its data to another text file.

Prerequisite:
In text file the data is stored in the ASCII format and the data can be read in the text editor with each alphabet in the file given a specific index in the memory similar to that of an array

Example:



// Sample input 1 
Input to the reverse function:
reverse

Output:
esrever

// Sample input 2
Input to the reverse function:  
Geeks For Geeks

Output:
skeeG roF skeeG

Approach:
Step 1: Calling reverse function with text that is needed to be contained in the first text file i.e. passing sample input of text file.
Working of Reverse funciton
Step 2: Opening file in writing mode and writing “str” into first text file here: in file Geeks.txt
Step 3: Storing the location of end of first text file in the variable “pos” and the closing the file.
Step 4: Opening first text file in reading mode and placing the reading pointer at position pos.
Step 5: Opening new text file “Geeks2.txt” in writing mode.
Step 6: Reading first text file character by character from the end and storing each character to the second text file.
Step 7: Shifting the read pointer one alphabet backwards in the text file.
Step 8: Closing the text files.
Step 9: Reading second text file (you can skip it if not necessary).

**name of first text file ="Geeks.txt"
**name of second text file="Geeks2.txt"

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <conio.h>
#include <fstream.h>
#include <iostream.h>
#include <stdio.h>
  
// function to perform the task
// accepts the parameter str as the text to
// be stored in text file
void reverse(char str[])
{
    char ch;
    ofstream ofs;
  
    // created text file
    ofs.open("Geeks.txt", ios::out);
  
    for (int i = 0; str[i] != '\0'; i++) {
        // writing into the file
        ofs.put(str[i]);
    }
  
    // storing the position of  end of the file
    int pos = ofs.tellp();
    ofs.close();
  
    // object for reading the contents of the
    // first text file
    ifstream ifs;
    ifs.open("Geeks.txt", ios::in);
  
    // object for writing in to the text file 2
    ofstream ofs1;
    ofs1.open("Geeks2.txt", ios::out);
  
    // putting the read pointer to the last alphabet
    // of the file
    ifs.seekg(--pos);
  
    while (pos >= 0) {
        // reading from the file 1
        ifs.get(ch);
  
        // writing to the file 2
        ofs1.put(ch);
  
        /* shifting read pointer position one 
        alphabet backwards in the text file */
        pos--;
  
        ifs.seekg(pos);
    }
  
    ifs.close();
    ofs1.close();
  
    ifstream ifs1;
    ifs1.open("Geeks2.txt", ios::in);
  
    // file.eof() checks the end of the text file
    while (!ifs1.eof()) {
        ifs1.get(ch);
        cout << ch;
    }
    ifs1.close();
}
  
// Driver code
int main()
{
    clrscr();
  
    // sample input 1
    cout << "example 1: (Geeks For Geeks) \n";
  
    // passing first text file's text
    // through reverse function
    reverse("Geeks For Geeks");
  
    // sample input 2
    cout << "\n example 2:(reverse)\n";
    reverse("reverse");
  
    getch();
    return 0;
}

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.