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-22T04:44:49-0500
Answer #include <iostream> #include <fstream> #include <string> using namespace std;
// Trying to open input file ifstream input(inputFileName); if (!input.is_open()) { cout << "Unable to open input file: " << inputFileName << endl; return -1; }
// Trying to open output file ofstream output(outputFileName); if (!output.is_open()) { cout << "Unable to open output file: " << outputFileName << endl; input.close(); return -1; }
// Read words and write resultant into output string word; while (input >> word) { if (is_valid_word(word)) output << word << " "; } // Close input.close(); output.close(); return 0; }
Comments
Leave a comment