I need to implement Huffman coding using a binary tree. I have successfully created the tree and have been able to execute the program by manually entering the characters and their frequencies but I now need to read the characters/frequencies from an input file and then output their matching Huffman codes to an output file. I believe that the problem lies in overloading the >> operator which would allow me to read in the values from the input file. Any help on this would be greatly appreciated.

#pragma warning(disable:4099)
#include<iostream>
#include<fstream>
#include<sstream>
#include "huffman.h"
#include <map>
#include <ostream>
#include <algorithm>
#include <iterator>
#include <string>

using namespace std;

ostream& operator<<(std::ostream& os, std::vector<bool> vec)
{
	std::copy(vec.begin(), vec.end(), std::ostream_iterator<bool>(os, ""));
	return os;
}

/*istream& operator>>(std::istream& is, std::vector<bool> vec)
{
	std::copy(vec.begin(), vec.end(), std::istream_iterator<bool>(is, ""));
	return os;
}*/

int main(int argc, char *argv[]) 
{
	ifstream iFile;
	ofstream oFile;

	map<char, double> frequencies;

	if (argc != 3) 
	{
		cerr << "Command requires <input_file> <output_file>" << endl;
		return EXIT_FAILURE;
	}
	else 
	{
		// open files
		iFile.open(argv[1]);
		oFile.open(argv[2]);
		
		if ( iFile.is_open() && oFile.is_open() ) 
		{
			
			while(!iFile.eof())
			{
				char char1;
				int	freq;

				iFile << char1 << freq;

				frequencies[char1] = freq;

				Hufftree<char, double> hufftree(frequencies.begin(), frequencies.end());

				for (char ch = 'a'; ch <= 'z'; ++ch)
				{
					oFile << ch << " " << hufftree.encode(ch) << "\n";
				}
			}
				
		}
		
		// close files
		iFile.close();
		oFile.close();
		return EXIT_SUCCESS;
	}
}

I don't think it makes sense to use << on an ifstream.

I think you should look into the getline() function. You shouldn't have to overload the >> operator to do this.

Dave

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.