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.
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
// 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
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 function
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"
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- C++ program to append content of one text file to another
- C++ program to modify the content of a Binary File
- C++program to delete the content of a Binary File
- lseek() in C/C++ to read the alternate nth byte and write it in another file
- How to store Data Triplet in a Vector in C++?
- How to store a very large number of more than 100 digits in C++
- Rotation of a point about another point in C++
- Check if a string can be formed from another string using given constraints
- How to convert a class to another class type in C++?
- Understanding ShellExecute function and it's application to open a list of URLs present in a file using C++ code
- Dividing a Large file into Separate Modules in C/C++, Java and Python
- Read a record from a File in C++ using seekg() and tellg()
- Different methods to reverse a string in C/C++
- Reverse an array upto a given position
- std::reverse() in C++
- Program to print reverse character bridge pattern
- forward_list::reverse() in C++ STL
- list reverse function in C++ STL
- How to traverse a STL map in reverse direction?
- How to traverse a C++ set in reverse direction
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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