Write a C++ code which writes the resultant words to the output file, “second_file.txt”.
Example: Content of first_file – “I am going to buy an umbrella”
Output in second_file.txt – I am an umbrella
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
int main()
{
std::ifstream inputFile("first_file.txt");
if (!inputFile.is_open())
{
std::cout << "Can\'t open input file" << std::endl;
return 0;
}
std::vector<std::string> words;
while (!inputFile.eof())
{
std::string s = "";
inputFile >> s;
words.push_back(s);
}
inputFile.close();
std::ofstream outputFile("second_file.txt");
outputFile << words.at(0) << " " << words.at(1) << " " << words.at(words.size() - 2) << " "
<< words.at(words.size() - 1) << std::endl;
outputFile.close();
}