File Handling is the storing of data in a file using a program. It enables us to create, update, read, and delete the files stored on the local file system through our program.
Today we are going to have a discussion where we are going to write a program in C++ to reverse the content of a file.
The fstream library allows us to work with files. There are three classes included in fstream library, which are used to create, write or read the files.
These are the data types used for file handling from the fstream library :
Data Type | Description |
---|---|
ofstream | It is used to create files and write on files. |
ifstream | It is used to read from files. |
fstream | It can perform functions of both other data types, it can create files, write on them and read. |
So, we are going to create an object of fstream to read from the file and take the content from the file in a string and reverse it. So, for opening the file we have to tell our machine the purpose of opening the file. For e.g.- to write on the file, to read from the file, etc. These are the different modes in which we can open a file :
These are few of the most common used modes :
Mode | Description |
---|---|
ios::app | opens the text file for appending in it. |
ios::in | opens the text file for reading |
ios::out | opens the text file for writing |
ios::trunc | truncates the content before opening a file, if file is present |
Approach 1
Our approach for solving this problem is very simple, all we have to do is to open a file, and then extract each words in our string variable. And then reverse it.
1. Open a file in reading mode
2. Extract words from it and store it in a string
3. Close the opened file
4. Reverse the string
5. Again open the file, this time in truncate and writing mode and add the string into the file.
6. Close the file.
Code
All programming languages have their own set of library functions in them. We can use any library function for reversing the string. For example, In C++, the standard library function can be used to reverse the string.
Below is the implementation for the above approach.
Note: We have already created a file codesdope.txt with text already written on it.
//C++ code
#include<bits/stdc++.h>
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
fstream file;
string str,line;
file.open("codesdope.txt",ios::in); // opening file
while(file >> str)
{
line = line + str;
line = line + " ";
}
file.close(); // closing file
cout<<"Line written on file : Before reversing"<<endl;
cout<<line<<endl;
reverse(line.begin(),line.end()); // reversing the string
file.open("codesdope.txt",ios::in|ios::out|ios::trunc);
file<<line<<endl; // writing the reversed string in the file
cout<<"Line written on file : After reversing"<<endl;
cout<<line<<endl;
file.close();
}
Output : Line written on file : Before reversing
epodsedoC ot emocleW
Line written on file : After reversing
Welcome to Codesdope
Approach 2
In, this approach we are going to modify the above code in the part where we were taking out every word from the file one by one, now we are going to take a full line using the getline function.
The C++ getline()
is a standard library function that is used to read a string or a line from an input stream. The getline() function extracts characters from the input stream and appends it to the string object until the delimiting character is encountered.
Rest, all the steps will be same. We are just extracting lines from the file not the word this time.
Code :
//C++ code
#include<bits/stdc++.h>
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
fstream file;
string str,line;
file.open("codesdope.txt",ios::in);
while(getline(file, str)){ //read data from file object and put it into string.
line = line + str;
line = line + "\n"; // adding new line in the string after every line
}
file.close();
cout<<"Line written on file : Before reversing"<<endl;
cout<<line<<endl;
reverse(line.begin(),line.end()); // reversing the string
file.open("codesdope.txt",ios::in|ios::out|ios::trunc);
file<<line<<endl;
cout<<"Line written on file : After reversing"<<endl;
cout<<line<<endl;
file.close();
}
Output : Line written on file : Before reversing
epodsedoC ot emocleW
Line written on file : After reversing
Welcome to Codesdope
Note: To perform file processing in C++, header files <iostream>
and <fstream>
must be included in your C++ source file. Also C++ automatically close and release all the allocated memory. But a programmer should always close all the opened files.