You\'re to write a C++ program that allows the user to enter the name of an inpu
ID: 3606884 • Letter: Y
Question
You're to write a C++ program that allows the user to enter the name of an input file and the name of an output file. If the filenames are identical, you should display an error message and shut down the program, otherwise use the filenames to open an input file and an output file. If both files are opened successfully, you're ready to pass them as arguments to the RevFile function.
The RevFile function will read the input file, one line at a time, and copy the lines to the output file. However, each output line will be copied to the output file in reverse order! So if the line "abc" is read from the input file, you'll write "cba" to the output file. Once the end of the input file has been reached, RevFile will return the total number of lines written to the caller.
How on earth will you do this? It's helpful to take some time to think about a few things:
How can you read an entire single line of text from an input stream? Remember a function called .getline()? That function will allow you to fetch a null-terminated cstring from the input stream and copy it to a destination character array.
The .open() member function for file stream objects allows you to open a file, and the argument we've always used for that function has been a string literal. Well, now that we know a string literal is really just an unnamed cstring, and the the .getline() function allows you to get a cstring from the user at the keyboard, this means you can now get the names of files to open from the user.
How will you write the string to the output stream in reverse? You should realize that the string will be contained in an array of chars, and that the array is null-terminated. So, all you have to do is derive the index location of the null character, and walk backwards through the string, one character at a time until you reach the first character, which will be at index location zero. (Hint: It's not required, but the strlen function might help you with this task...).. Please make comments in order for me to know what each line of code does
Explanation / Answer
The explanation of each line has been given as comments. It will be clear once you go through the code. If you have any further doubts. Kindly comment below.
*****************************************************************************************************************************
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
void reverseStr(string &str)
{
int n = str.length();
// Swap character starting from two
// corners
for (int i=0; i<n/2; i++)
swap(str[i], str[n-i-1]);
}
int main() {
//2string variables to take user input of the 2 files.
std::string inputFile,outputFile;
cout << "Enter input file name: ";
//User input of input file name
cin >> inputFile;
cout << "Enter output file name: ";
//User input of output file name
cin >> outputFile;
//Checking if both file names are same or not
if((inputFile.compare(outputFile)) == 0){
cout << "Input File and Output File cannot be the same." << endl;
exit(1);
}
ifstream inFile;
ofstream outFile;
//Opening both files
inFile.open(inputFile);
outFile.open (outputFile);
//Exit if file cannot be opened
if (!inFile) {
cout << "Unable to open input file";
exit(1); // terminate with error
}
//Exit if file cannot be opened
else if(!outFile){
cout << "Unable to open output file";
exit(1); // terminate with error
}
//Reading each line from input file
for( std::string line; getline( inFile, line ); )
{
//Reverse the line
reverseStr(line);
//Write to output file
outFile << line << endl;
}
//Closing the files.
inFile.close();
outFile.close();
cout << "File successfully copied in reverse order." << endl;
return 0;
}
*************************************************************************************************************************
Sample Output 1:
Sample output 2:
test.txt
output.txt
**********************************************************************************************************************
I hope this helps you.
If you find my answer helpful,
Kindly rate the answer.
All the best :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.