// Asc2Bin - Convert ASCII files to binary files.
// By DreamVB 23:06 17/10/2016

#include <iostream>
#include <fstream>
#include <string>

using namespace std;
using std::cout;
using std::endl;

int StrToInt(char *src){
	int i = 0;
	int value = 0;

	while (src[i] != '\0'){
		value = value * 10 + (src[i] - '0');
		i++;
	}

	return value;
}

int main(int argc, char *argv[]){
	fstream fs;
	ofstream fo;
	int ch = 0;
	int len = 0;
	char num[10];
	std::string line = "";

	//Check file parms
	if (argc != 3){
		cout << "Converts a ASCII file to binary." << endl << endl;
		cout << "Usage: " << argv[0] << " <source><destination>" << endl;
		exit(1);
	}

	//Open input file
	fs.open(argv[1],ios::in);

	//Check if input file is opened.
	if (!fs){
		cout << "Input read error." << endl;
		exit(1);
	}

	//Open output file
	fo.open(argv[2],ios::out | ios::binary);

	//Check if output file was opened.
	if (!fo){
		cout << "Output file can not be created." << endl;
		fs.close();
		exit(1);
	}

	//Read in the lines from the ASCII file.
	while (std::getline(fs, line)){
		strcpy(num, line.c_str());

		if (strlen(num)){
			//Convert number to char
			ch = StrToInt(num);
			//Write the char value to the output file.
			fo.put(ch);
		}
	}

	//Close output and input file.
	fo.close();
	fs.close();

	return 0;
}