/* Chapter 11, exercise 12: reverse the order of characters in a text file
 (hint: "file open modes")
*/

#include "std_lib_facilities.h"

int main() {
	cout << "Enter input file name: ";
	string ifname;
	cin >> ifname;
	ifstream ifs(ifname.c_str());
	if (!ifs) error("can't open input file ", ifname);
	cout << "Enter output file name: ";
	string ofname;
	cin >> ofname;
	ofstream ofs(ofname.c_str());
	if (!ofs) error("can't open output file ", ofname);

	// read file into string
	string contents;
	char ch;
	while (ifs.get(ch))
		contents.push_back(ch);

	// read string from end, write into file
	for (int i = contents.size() - 1; i >= 0; --i)
		ofs << contents[i];
}