Answer:
See explaination
Step-by-step explanation:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int size = 10;
string inputFileName, outputFileName;
cout << "Please enter input file name, including full path: ";
cin >> inputFileName;
cout << "Please enter output file name, including full path: ";
cin >> outputFileName;
ifstream inFile(inputFileName.c_str());
ofstream outFile(outputFileName.c_str());
// checking whether input and output files are good to open
if(!inFile.is_open())
{
cout << "Cannot open " << inputFileName << endl;
exit(EXIT_FAILURE);
}
if(!outFile.is_open())
{
cout << "Cannot open " << outputFileName << endl;
exit(EXIT_FAILURE);
}
// declare an array of string to store 1024 words
string words[size];
// read the file for exactly 1024 words
// assuming each line of input file contains only 1 word
int count = 0;
string word;
while(getline(inFile, word))
{
if(count == size)
break;
words[count++] = word;
}
inFile.close();
// now we need to print the words array in reverse order both to the console and to the output file, simultaneously
cout << "WORDS:\\------\\";
for(int i = size - 1; i >= 0; i--)
{
if(words[i] != "")
{
cout << words[i] << endl;
outFile << words[i] << endl;
}
}
outFile.close();
system("pause");
return 0;
}