Beispiel #1
0
int main(int argc, char* argv[]) {
    using namespace ptlmuh006;

    if(argc != 4){
        std::cout << "Incorrect usage." << std::endl;
        std::cout << "Please use huffencode as follows:" << std::endl;
        std::cout << "huffencode <option> <inputFilename> <outputFilename>" << std::endl;
        std::cout << "\t___options:___" << std::endl;
        std::cout << "\t -c compress data from inputFilename to outputFilename" << std::endl;
        std::cout << "\t -x extract compressed data from inputFilename to outputFilename" << std::endl;

    }else{
        HuffmanTree* huff = new HuffmanTree;

        std::string option = argv[1];
        if(option == "-c"){
            huff->compress(argv[2], argv[3]);
            std::cout << "Compressed " << argv[2] << " into " << argv[3] << "." << std::endl;

        }else if(option == "-x"){
            huff->extract(argv[2], argv[3]);
            std::cout << "Extracted from " << argv[2] << " into " << argv[3] << "." << std::endl;
        }

        delete huff;
    }
    return 0;
}
Beispiel #2
0
int main(){
	ifstream file;
	file.open("Amazon_EC2.txt");
	if(!file.is_open()) cout << "File not found";

	//Create a vector of freqs
	//256 for ascii values initialized to 0
	vector<int> freq(256,0);

	int c;
	int count = 0;
	while(file.good()){
		count++;
		c = file.get();
		//End of file
		if(c < 0) continue;
		freq[c] = freq[c] + 1;
	}
	file.close();

	/*
	for(int i = 0; i < freq.size(); i++){
		if(freq[i] != 0){
			double ratio = ((double)freq[i]/(double)count)*100;
			cout << (char)i <<  " " << ratio << endl;
		}
	}
	*/

	//Instantiate tree
	HuffmanTree tree;
	//Call create to make the tree 
	tree.create(freq);

	//Create output file stream
	ofstream outputFile("compressedFile.txt",ios::out);	
	stream out(outputFile);

	//Open file to get letters to compress with path
	ifstream file2;
	file2.open("Amazon_EC2.txt");

	//Call compress to write bits of each letter to the file
	char z;
	while(file2.good()){
		z = (char)file2.get();	
		tree.compress(z, out);
	}

	file2.close();
	outputFile.close();
	cout << endl << "Outputfile Created: compressedFile.txt" << endl << endl;
	return 0;	
}