#include <fstream>
#include <iostream>
#include <map>

int main(int argc, char* argv[])
{
	if ( argc <= 2 ) { std::cout << "Usage: word_count SOURCE TARGET\n"; return 0; }

	std::ifstream source(argv[1]);
	std::ofstream target(argv[2]);

	if ( ! source ) { std::cerr << "word_count: could not open " << argv[1] << std::endl; return 1; }
	if ( ! target ) { std::cerr << "word_count: could not open " << argv[2] << std::endl; return 2; }

	std::map<std::string, size_t> timesOf;
	std::string word;
	while ( source >> word )
		++timesOf[word];

	for ( std::map<std::string, size_t>::const_iterator itr = timesOf.begin(); itr != timesOf.end(); ++itr )
		target << itr->first << '\t' << itr->second << std::endl;

	source.close();
	target.close();
}
