67 014
Assignments Done
99,4%
Successfully Done
In November 2018

Answer to Question #72110 in C++ for Dhvani

Question #72110
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
Expert's answer
#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();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions