Error 6 error C2143: syntax error : missing ';' before ')' c:\users\datacompress.cpp 214 1
Error 8 error C2039: 'putSymbol' : is not a member of 'std::basic_ofstream<_Elem,_Traits>' c:\users\datacompress.cpp 291 1
Error 9 IntelliSense: class "std::basic_ofstream<char, std::char_traits<char>>" has no member "putSymbol" c:\users\datacompress.cpp 291 12
#include <fstream>
#include <string>
#include <iostream>
#include <ostream>
using namespace std;
ifstream infile ("File.txt", ios::in | ios::binary);
ofstream outfile("Fileoutput.txt", ios::out|ios::binary);
int freq;
unsigned char symbol;
char leftLeaf;
char rightLeaf;
void hufftree();
int compare;
int* lptr;
int* rptr;
char root;
void putSymbol(char * code);
void putBit();
class NODE
{
friend class TREE;
private:
int freq;
int symbol;
char * code;
bool scannedNode;
public:
NODE * nextNode;
NODE()
{
if(lptr, rptr = NULL)
{
freq = 0;
scannedNode = 0;
nextNode = NULL;
symbol = -1;
}
}
NODE * newNode(unsigned char);
void increaseNode()
{
freq++;
}
NODE(unsigned char chr)
{
while(lptr, rptr, nextNode = NULL)
{
scannedNode = 0;
NODE::freq = 1;
symbol = chr;
}
}
NODE * lptr;
NODE * rptr;
NODE * TREE;
};
NODE * NODE::newNode(unsigned char chr)
{
NODE * curNode;
curNode = this;
for(curNode = this; curNode != NULL; curNode = curNode -> nextNode)
{
if(curNode ->symbol == chr)
{
curNode->increaseNode();
break;
}
}
return curNode;
};
class TREE
{
public:
NODE * headLeaf;
NODE * getmin();
void Binary(NODE *);
void Create();
void Insert(unsigned char);
void printTree(NODE * n);
TREE();
NODE * root; //where root of binary tree, 1st node in linked list
NODE * firstNode;
char * getCode(unsigned char);
};
NODE * TREE::getmin()
{
NODE * curNode;
NODE * minNode = NULL;
int bestNode;
if(curNode = headLeaf)
{
curNode != NULL;
curNode = curNode->nextNode; //get next node in list
while(!curNode -> scannedNode)
{
minNode == NULL || curNode -> freq < bestNode;
minNode = curNode;
bestNode = curNode->freq;
}
}
if(minNode != NULL)
{
minNode -> scannedNode = 1;
return minNode;
}
}
void TREE::Binary(NODE * n)
{
int level;
static char outputstr[50] = {0};
static char * code = &outputstr[2]; //goto left
//while(level != NULL)
//{
if(n->lptr != NULL)
{
strcat(code, "0"); //outputstr
Binary(n->lptr);
printTree(n->lptr);
outputstr[strlen(outputstr)-1] = 0; // restore.
}
if(n->rptr != NULL) //else
{
strcat(code, "1");//outputstr
Binary(n->rptr);
printTree(n->rptr);
outputstr[strlen(outputstr)-1] = 0; // restore.
}
n-> code = new char[strlen(code) + 1];
strcpy(n->code, outputstr);
code[strlen(code)-1] = NULL;
if(n->lptr == NULL)
{
if(strlen(n->code) < 8)
cout << (int) n->symbol << "\t\t" << n-> code << "\t\t\t" << n->freq << "\t\t" << (char) n->symbol << endl;
else
cout << (int) n-> symbol << "\t\t" << n->code << "\t\t" << n->freq << "\t\t" << (char) n->symbol << endl;
}
};
void TREE::Create()
{
NODE * leftLeaf=NULL;
NODE * rightLeaf=NULL;
NODE * insertLeaf;
while(1)
{
leftLeaf = getmin();
rightLeaf = getmin();
if(leftLeaf == NULL)
{
root = rightLeaf; //root of huffman tree, not a pointer
return;
}
insertLeaf = new NODE();
insertLeaf->nextNode = headLeaf;
headLeaf = insertLeaf;
insertLeaf ->lptr = leftLeaf;
insertLeaf->rptr = rightLeaf;
insertLeaf->freq = leftLeaf->freq + rightLeaf->freq;
}
};
char * TREE::getCode(unsigned char c)
{
for(NODE * curNode = headLeaf; curNode != NULL; curNode =curNode ->nextNode)
if(curNode->symbol == c && curNode ->lptr == NULL)
return curNode -> code;
return NULL;
};
void TREE::Insert(unsigned char s)
{
NODE * curNode;
NODE * insertLeaf;
curNode = headLeaf;
if(curNode->newNode(s) == NULL)//s not read before
{
insertLeaf = new NODE(s);
insertLeaf -> nextNode = headLeaf;
headLeaf = insertLeaf;
}
}
class bitstream: public ofstream
{
public:
bitstream(char * b, unsigned t): ofstream(b,t)
{
}
int i;
void putSymbol(char * code)
{
for(int i = 0; code[i] !=0, i++)
putBit(code[i]-48);
}
private:
void putBit(char s)
{
static unsigned char byte = NULL;
static int position = 7;
byte = byte | (s << position--);
if(position ==1)
{
this->put(byte);
byte = NULL;
position == 7;
}
}
};
void TREE::printTree(NODE * n)
{
static char outputstr[50];
if(n->rptr)
{
strcat(outputstr, "1");
printTree(n->rptr);
outputstr[strlen(outputstr)-1] = 0; // restore.
}
if(n->lptr)
{
strcat(outputstr, "0");
printTree(n->lptr);
outputstr[strlen(outputstr)-1] = 0; // restore.
}
root = NULL;
headLeaf = NULL;
};
int main()
{
int a[300];
TREE hufftree;
//create hufftree;
/*ifstream infile;
infile.open("File.txt", fstream::in);
string * leafIn;
string * leafOut;*/
string line;
while(1)
{
symbol = infile.get();
if(infile.eof())
break;
hufftree.Insert(symbol);
}
hufftree.Create();
cout<< "Symbol as (int) Huffman Code\tFrequency\tCharacter version of symbol\n\n";
hufftree.Binary(hufftree.root);
infile.clear();
infile.seekg(0,ios::beg);
while(1)
{
symbol = infile.get();
if(infile.eof())
break;
char * c = hufftree.getCode(symbol);
if (c)
outfile.putSymbol(c);
}
cout << "Output file size" << outfile.tellp() << endl;
outfile.close();
infile.close();
}