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
1
Expert's answer
2017-12-24T10:35:06-0500
#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 the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS