concatenating files containing whitespace-separated words

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 10 Exercise 9
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PPP
//	Chapter 10 Exercise 9

#include "std_lib_facilities.h"

void read_file(string& _input, ofstream& _ost) {
	ifstream ist{ _input };
	string result;
	if (!ist) error("can't open input file ", _input);
	ist.exceptions(ist.exceptions() | ios_base::badbit);
	ist >> result;

	while (true) {
		_ost << result << endl;
		if (ist.eof()) {
			break;
		}
		else {
			ist >> result;
		}
	}
}

void write_file(string& _output, string& _input_1, string& _input_2) {
	ofstream ost{ _output };
	if (!ost) error("can't open output file", _output);

	read_file(_input_1, ost);
	read_file(_input_2, ost);
}

void calculate() {
	string input_1 = "input/ch10_9_input_1.txt";
	string input_2 = "input/ch10_9_input_2.txt";
	string output = "output/ch10_9_output_1+2.txt";
	
	cout << "Combining files " << input_1 << " & " << input_2 << "...";
	write_file(output, input_1, input_2);
	cout << " Done!" << endl;
	cout << output << " successfully written." << endl;
}

int main()
try
{
	calculate();

	keep_window_open();
}
catch (runtime_error e) {
	cout << e.what() << endl;
	keep_window_open();
}
catch (...) {
	cout << "Exiting" << endl;
	keep_window_open();
}
Output:
Combining files input/ch10_9_input_1.txt & input/ch10_9_input_2.txt... Done!
output/ch10_9_output_1+2.txt successfully written.
Please enter a character to exit
Input-File 1: ch10_9_input_1.txt
this is a test
Input-File 2: ch10_9_input_2.txt
 and so is this
Output-File: ch10_9_output_1+2.txt
this
is
a
test
and
so
is
this

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.