Hi every body...
this(attachment) is my code for implemention of Huffman code,this code must read every file and compress them,
it works on many test file but if I input test.txt it give an Error...
How can i solve this error ...
and the next problem is if we input a gif file (for instance 1.gif) it doesn't give any output!..
Can any body help me how to change the code to read for every file ?
I excuse for my question that take up much time!...
(I am so ashamed but I can't find any remedy!)
tnx so much.
moein_soltani 0 Newbie Poster
The attachment preview is chopped off after the first 10 KB. Please download the entire file.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include<stdio.h>
using namespace std;
char file_name[100];
struct Node{
int q;
char symbol;
Node *right;
Node *left;
};
//################################################################################################
struct Code{
char symbol;
string code;
int q;
};
//################################################################################################
//################################################################################################
class Huffman{
public:
Huffman();
Huffman(int);
~Huffman(){
cout<<"\nDestructor executed!";
}
int find_dissimilar_chars();
void find_Quantity();
int find_symbol(char);
void print();
void swap(Node &y,Node &x);
void Min_heap();
void Min_heapify(int);
void heap_sort();
void make_tree();
void decoder();
int find_sym(char sym);
void coder();
void build_Binary();
void file_size1();
void file_size2();
void printTree(Node*,int);
inline void displayTree(){
printTree(&root,0);
}
void set_code(Node *current_Node,string code="");
void printTree2(Node*,int);
void padding(char ch,int n);
void displayTree2();
void readFile();
//void writeFile();
void build_compressed();
private:
Code *codes_list;
int heap_size,size,length;
Node *list;
Node *tree_list;
Node root;
int left_chars;
char * memblock;
long end;
};
//###############################################################################################
Huffman::Huffman(){
readFile();
size=find_dissimilar_chars();
list=new Node [size];
codes_list=new Code[size];
tree_list=new Node[1000];
left_chars=0;
char tmp;
int k=1,c;
for(int r=0;r<end;r++){
tmp=memblock[r];
for ( c=1;c<k;c++){
if(list[c].symbol==tmp)
break;
}
if (c==k){
list[k].symbol=tmp;
codes_list[k].symbol=tmp;
list[k].q=codes_list[k].q=0;
list[k].right=list[k].left=0;
k++;
}
}
length=size-1;
}
Huffman::Huffman(int){
}
void Huffman::padding(char ch,int n){
int i;
for ( i = 0; i < n; i++ )
putchar ( ch );
}
void Huffman::find_Quantity(){
char tmp;
for(int r=0;r<end;r++){
tmp=memblock[r];
int k=find_symbol(tmp);
++ list[k].q;
++codes_list[k].q;
}
}
int Huffman::find_symbol(char tmp){
for(int k=1;k<=length;k++){
if (list[k].symbol==tmp)
return k;
}
return 0;
}
void Huffman::Min_heapify(int m){
int l=2*m;
int r=2*m+1;
int minimum;
if(l<=heap_size && list[l]. q < list[m].q)
minimum=l;
else
minimum=m;
if ( r<=heap_size && list[r].q < list[minimum].q)
minimum=r;
if (minimum!=m){
swap(list[minimum],list[m]);
Min_heapify(minimum);
}
}
void Huffman::Min_heap(){
heap_size=length;
for (int k = ( length/2 );k>0;k--)
Min_heapify(k);
}
void Huffman::print(){
cout<<"The Quantity Of character in the input.txt is:\n";
for(int k=1;k<=length;k++){
switch (list[k].symbol){
case (10):
cout<<endl<<left<<setw(6)<<k<<setw(4)<<left<<"Enter"<<setw(10)<<left
<<setw(7)<<right<<list[k].q<<endl;
break;
case(32):
cout<<endl<<left<<setw(6)<<k<<setw(4)<<left<<"Space"<<setw(10)<<left
<<setw(7)<<right<<list[k].q<<endl;
break;
default:
cout<<endl<<left<<setw(6)<<k<<setw(4)<<left<<list[k].symbol<<setw(10)<<left
<<setw(7)<<right<<list[k].q<<endl;
}
}
cout<<"\n\n\n";
//for(int r=0;r<end;r++)
// cout<<memblock[r];
cout<<right<<"\n\t-Print() executed ";
}
void Huffman::swap(Node &y , Node &x){
Node temp=y;
y=x;
x=temp;
}
int Huffman::find_dissimilar_chars(){
char tmp,tmp2[256];
int k=1,c;
for(int r=0;r<end;r++){
tmp=memblock[r];
for ( c=1;c<k;c++){
if(tmp2[c]==tmp)
break;
}
if (c==k){
tmp2[k]=tmp;
k++;
}
}
return k;
}
void Huffman::heap_sort(){
for(int k=length;k>1;k--){
swap(list[1],list[k]);
heap_size--;
Min_heapify(1);
}
//heap_size=length;
}
void Huffman::make_tree(){
int c=0;
while(length!=1){
tree_list[c]=list[length];
tree_list[c+1]=list[length-1];
length--;
list[length].q+=list[length+1].q;
list[length].symbol=0;
list[length].left=&tree_list[c];
list[length].right=&tree_list[c+1];
if (length!=1){
int tmplength=length;
while(list[tmplength].q>list[tmplength-1].q && tmplength!=1){
swap(list[tmplength],list[tmplength-1]);
tmplength--;
}
}
c=c+2;
//print();
}
root=list[1];
delete []list;
}
void Huffman::set_code(Node *current_Node,string code){
if ((current_Node->symbol) !=0 ){
int c=find_sym(current_Node->symbol);
codes_list[c].code=code;
return;
}
else{
if (current_Node->left!=0){
set_code(current_Node->left,code+'0');
}
if(current_Node->right!=0){
set_code(current_Node->right,code+'1');
}
}
}
int Huffman::find_sym(char sym){
for(int k=1;k<size;k++){
if (codes_list[k].symbol==sym)
return k;
}
cout<<"0 was returned";
return 0;
}
void Huffman::coder(){
set_code(&root);
cout<<"The Address of each character is:\n";
for(int k=1;k<size;k++)
cout<<codes_list[k].symbol<<setw(20)<<codes_list[k].code<<endl;
}
void Huffman::build_Binary(){
ofstream output("Binary.txt");
char tmp;
int c;
string str1;
for(int r=0;r<end;r++){
tmp=memblock[r];
c=find_sym(tmp);
str1=codes_list[c].code;
for(int k=0; str1[k]!=0 ; k++){
switch (str1[k]){
case '0':
output<<0;
//output.write((char*)&zero,1.0/4);
break;
case '1':
output<<'1';
//output.write((char*)&one,1.0/4);
break;
}
}
}
delete [] memblock;
cout<<"\nBinary File was Built\n";
}
void Huffman::decoder(){
ifstream input("compressedInput.txt");
list=new Node [255];
tree_list=new Node [1000];
codes_list=new Code[255];
long position,l;
left_chars=input.get();
size=int(input.get());
char ch;
int t=1;
while(t<size){
ch=input.get();
codes_list[t].symbol=list[t].symbol=ch;
input>>list[t].q;
list[t].right=list[t].left=0;
t++;
input.get();
}
position=input.tellg();
input.close();
length=size-1;
Min_heap();
heap_sort();
print();
make_tree();
set_code(&root,"");
input.open("compressedInput.txt",ios::in|ios::binary);
input.seekg(position);
input.seekg(0,ios::end);
end=input.tellg();
l=end-position;
memblock=new char [l];
input.seekg(position);
input.read(memblock,l);
ofstream output("Output.txt");
ofstream Binary("Binary2.txt");
for(int r=0;r<l;r++){
char byte=memblock[r];
if (r==l-1){
char left_bits[8];
char mask=1;
mask=mask<<7;
char bit;
for(int k=0;k<8;k++){
bit=( byte & mask ? '1' : '0' );
left_bits[k]=bit;
byte=byte<<1;
}
for(int k=0;k<left_chars;k++)
Binary<<left_bits[k];
}
else{
char mask=1;
mask=mask<<7;
char bit;
for(int k=0;k<8;k++){
bit=( byte & mask ? '1' : '0' );
Binary<<bit;
byte=byte<<1;
}
}
}
Binary.close();
delete[] memblock;
ifstream Binary2("binary2.txt");
char tmp2;
Node *p=&root;
while(Binary2>>tmp2){
switch (tmp2){
case '0':
p=p->left;
break ;
case '1':
p=p->right;
break;
}
if (p->symbol!=0){
output<<p->symbol;
p=&root;
}
}
Binary2.close();
// if (!remove("binary2.txt"))
// cout<<"\nbinary2.txt has been removed.\n";
}
void Huffman::file_size1(){
ifstream output("input.txt");
long begin,end;
begin = output.tellg();
output.seekg (0, ios::end);
end = output.tellg();
output.close();
cout << "Input File size is: " << (end-begin) << " bytes.\n\n";
}
void Huffman::file_size2(){
ifstream output("compressedInput.txt");
long begin,end;
begin = output.tellg();
output.seekg (0, ios::end);
end = output.tellg();
output.close();
cout <<setw(50)<< "compressedInput size is: " << (end-begin) << " bytes.\n\n";
}
void Huffman::printTree(Node *root,int indent){
if (root!=0){
printTree(root->right,indent+4);
cout << setw(indent) << " " << root->symbol << endl;
printTree(root->left, indent + 4);
}
}
void Huffman::printTree2(Node *root,int level){
if ( root == 0 ) {
padding ( '\t', level );
puts ( "~" );
}
else {
I STAND ALONE
I HAVE TOLD YOU THIS ONCE BEFORE CANT CONTROL ME
IF YOU TRY TO TAKE ME DOWN YOU ARE GONNA BREAK
I FEEL YOU ARE EVERY NOTHING THAT YOU ARE DOING FOR ME
I AM THINKING YOU OUTTA MAKE YOUR OWN WAY
I STAND ALONE INSIDE
YOU ARE ALWAYS HIDING BEHIND YOU ARE
SOCALLED GODDESS
SO WHAT YOU DONT THINK THAT WE CAN SEE YOUR FACE
RESSURECTED BACK BEFORE THE FINAL FALLING
I WILL NEVER REST UNTIL I CAN MAKE MY OWN WAY
I AM NOT AFRAID OF FADING
I STAND ALONE
FEELING YOUR STING DOWN INSIDE ME
I AM NOT DYING FOR IT
I STAND ALONE
EVERYTHING THAT I BELIEVE IS FADING
I STAND ALONE INSIDE
AND NOW ITS MY TIME ITS MY TIME TO DREAM DREAM OF THE SKIES
MAKE ME BELIEVE THAT THIS PLACE ISNT PLAGUED BY THE POISON IN ME
AND HELP ME DECIDE IF MY FIRE WILL BURN OUT BEFORE YOU CAN BREATHE INTO MEEND
Salem 5,199 Posting Sage
Well right at the start
Huffman::Huffman
end is uninitialised.
Then later on...
long begin,end;
begin = output.tellg();
output.seekg (0, ios::end);
end = output.tellg();
This local variable shadows your member variable, and you're still out of luck.
moein_soltani 0 Newbie Poster
excuse me,I didn't understand your help!...
I think uninitialized end doesn't make any problem...
May you correct the mistakes and upload cpp file again ?
tnx.
Salem 5,199 Posting Sage
> I think uninitialized end doesn't make any problem...
Well you could try printing it as well, just to see how badly you're screwing up.
> May you correct the mistakes and upload cpp file again ?
Nope.
We point out problems, you fix them.
moein_soltani 0 Newbie Poster
Well right at the start
Huffman::Huffman
end is uninitialised.Then later on...
long begin,end; begin = output.tellg(); output.seekg (0, ios::end); end = output.tellg();
This local variable shadows your member variable, and you're still out of luck.
If you notice I have initialized end in readFile() function.
May you explain more ?...
where must i add this code ? and what how can i solve other problems that i mentioned.
Edited by moein_soltani because: n/a
Salem 5,199 Posting Sage
<<nevermind, I have nothing to add to this discussion - code is too messy>>
Edited by Salem because: n/a
moein_soltani 0 Newbie Poster
It's uninitialised in the constructor(which is the FIRST thing that happens), then you promptly go and USE that value in your constructor.
Your program is now all but dead, and any results you may see after this point are suspect.
But Notice that the first thing in constructor is readFile.and it initializes it.
Dave Sinkula 2,398 long time no c Team Colleague
Here are some things to consider looking into:
--- Module: main.cpp (C++)
main.cpp 38 error 1551: (Warning -- Function may throw exception '...' in destructor 'Huffman::~Huffman(void)')
main.cpp 39 error 1540: (Warning -- Pointer member 'Huffman::codes_list' (line 69) neither freed nor zeroed by destructor)
main.cpp 69 error 830: (Info -- Location cited in prior message)
main.cpp 39 error 1540: (Warning -- Pointer member 'Huffman::list' (line 71) neither freed nor zeroed by destructor)
main.cpp 71 error 830: (Info -- Location cited in prior message)
main.cpp 39 error 1540: (Warning -- Pointer member 'Huffman::tree_list' (line 72) neither freed nor zeroed by destructor)
main.cpp 72 error 830: (Info -- Location cited in prior message)
main.cpp 39 error 1540: (Warning -- Pointer member 'Huffman::memblock' (line 75) neither freed nor zeroed by destructor)
main.cpp 75 error 830: (Info -- Location cited in prior message)
main.cpp 84 error 1732: (Info -- new in constructor for class 'Huffman' which has no assignment operator)
main.cpp 84 error 1733: (Info -- new in constructor for class 'Huffman' which has no copy constructor)
main.cpp 84 error 737: (Info -- Loss of sign in promotion from int to unsigned long)
main.cpp 85 error 737: (Info -- Loss of sign in promotion from int to unsigned long)
main.cpp 99 error 725: (Info -- Expected positive indentation from line 98)
main.cpp 98 error 830: (Info -- Location cited in prior message)
main.cpp 100 error 725: (Info -- Expected positive indentation from line 98)
main.cpp 98 error 830: (Info -- Location cited in prior message)
main.cpp 101 error 725: (Info -- Expected positive indentation from line 98)
main.cpp 98 error 830: (Info -- Location cited in prior message)
main.cpp 102 error 725: (Info -- Expected positive indentation from line 98)
main.cpp 98 error 830: (Info -- Location cited in prior message)
main.cpp 103 error 725: (Info -- Expected positive indentation from line 98)
main.cpp 98 error 830: (Info -- Location cited in prior message)
main.cpp 112 error 1401: (Warning -- member 'Huffman::codes_list' (line 69) not initialized by constructor)
main.cpp 69 error 830: (Info -- Location cited in prior message)
main.cpp 112 error 1401: (Warning -- member 'Huffman::heap_size' (line 70) not initialized by constructor)
main.cpp 70 error 830: (Info -- Location cited in prior message)
main.cpp 112 error 1401: (Warning -- member 'Huffman::size' (line 70) not initialized by constructor)
main.cpp 70 error 830: (Info -- Location cited in prior message)
main.cpp 112 error 1401: (Warning -- member 'Huffman::length' (line 70) not initialized by constructor)
main.cpp 70 error 830: (Info -- Location cited in prior message)
main.cpp 112 error 1401: (Warning -- member 'Huffman::list' (line 71) not initialized by constructor)
main.cpp 71 error 830: (Info -- Location cited in prior message)
main.cpp 112 error 1401: (Warning -- member 'Huffman::tree_list' (line 72) not initialized by constructor)
main.cpp 72 error 830: (Info -- Location cited in prior message)
main.cpp 112 error 1401: (Warning -- member 'Huffman::root' (line 73) not initialized by constructor)
main.cpp 73 error 830: (Info -- Location cited in prior message)
main.cpp 112 error 1401: (Warning -- member 'Huffman::left_chars' (line 74) not initialized by constructor)
main.cpp 74 error 830: (Info -- Location cited in prior message)
main.cpp 112 error 1401: (Warning -- member 'Huffman::memblock' (line 75) not initialized by constructor)
main.cpp 75 error 830: (Info -- Location cited in prior message)
main.cpp 112 error 1401: (Warning -- member 'Huffman::end' (line 76) not initialized by constructor)
main.cpp 76 error 830: (Info -- Location cited in prior message)
main.cpp 117 error 725: (Info -- Expected positive indentation from line 116)
main.cpp 116 error 830: (Info -- Location cited in prior message)
main.cpp 118 error 1762: (Info -- Member function 'Huffman::padding(char, int)' could be made const)
main.cpp 134 error 725: (Info -- Expected positive indentation from line 133)
main.cpp 133 error 830: (Info -- Location cited in prior message)
main.cpp 137 error 1762: (Info -- Member function 'Huffman::find_symbol(char)' could be made const)
main.cpp 183 error 1762: (Info -- Member function 'Huffman::print(void)' could be made const)
main.cpp 189 error 1762: (Info -- Member function 'Huffman::swap(struct Node &, struct Node &)' could be made const)
main.cpp 200 error 530: (Warning -- Symbol 'tmp2' (line 195) not initialized)
main.cpp 195 error 830: (Info -- Location cited in prior message)
main.cpp 204 error 725: (Info -- Expected positive indentation from line 203)
main.cpp 203 error 830: (Info -- Location cited in prior message)
main.cpp 205 error 725: (Info -- Expected positive indentation from line 203)
main.cpp 203 error 830: (Info -- Location cited in prior message)
main.cpp 209 error 1762: (Info -- Member function 'Huffman::find_dissimilar_chars(void)' could be made const)
main.cpp 266 error 725: (Info -- Expected positive indentation from line 264)
main.cpp 264 error 830: (Info -- Location cited in prior message)
main.cpp 273 error 1746: (Info -- parameter 'code' in function 'Huffman::set_code(struct Node *, std::basic_string<char>)' could be made const reference)
main.cpp 273 error 818: (Info -- Pointer parameter 'current_Node' (line 255) could be declared as pointing to const)
main.cpp 255 error 830: (Info -- Location cited in prior message)
main.cpp 283 error 1762: (Info -- Member function 'Huffman::find_sym(char)' could be made const)
main.cpp 306 error 732: (Info -- Loss of sign (arg. no. 2) (int to unsigned long))
main.cpp 307 error 732: (Info -- Loss of sign (arg. no. 2) (int to unsigned long))
main.cpp 316 error 744: (Info -- switch statement has no default)
main.cpp 325 error 423: (Warning -- Creation of memory leak in assignment to 'Huffman::list')
main.cpp 326 error 423: (Warning -- Creation of memory leak in assignment to 'Huffman::tree_list')
main.cpp 327 error 423: (Warning -- Creation of memory leak in assignment to 'Huffman::codes_list')
main.cpp 337 error 734: (Info -- Loss of precision (assignment) (31 bits to 7 bits))
main.cpp 349 error 712: (Info -- Loss of precision (assignment) (long long to long))
main.cpp 361 error 747: (Info -- Significant prototype coercion (arg. no. 1) long to long long)
main.cpp 362 error 747: (Info -- Significant prototype coercion (arg. no. 1) int to long long)
main.cpp 363 error 712: (Info -- Loss of precision (assignment) (long long to long))
main.cpp 365 error 737: (Info -- Loss of sign in promotion from long to unsigned long)
main.cpp 366 error 747: (Info -- Significant prototype coercion (arg. no. 1) long to long long)
main.cpp 377 error 701: (Info -- Shift left of signed quantity (int))
main.cpp 377 error 734: (Info -- Loss of precision (assignment) (14 bits to 7 bits))
main.cpp 382 error 701: (Info -- Shift left of signed quantity (int))
main.cpp 382 error 734: (Info -- Loss of precision (assignment) (8 bits to 7 bits))
main.cpp 389 error 725: (Info -- Expected positive indentation from line 387)
main.cpp 387 error 830: (Info -- Location cited in prior message)
main.cpp 389 error 701: (Info -- Shift left of signed quantity (int))
main.cpp 389 error 734: (Info -- Loss of precision (assignment) (14 bits to 7 bits))
main.cpp 394 error 701: (Info -- Shift left of signed quantity (int))
main.cpp 394 error 734: (Info -- Loss of precision (assignment) (8 bits to 7 bits))
main.cpp 414 error 744: (Info -- switch statement has no default)
main.cpp 428 error 578: (Warning -- Declaration of symbol 'end' hides symbol 'Huffman::end' (line 76))
main.cpp 76 error 830: (Info -- Location cited in prior message)
main.cpp 429 error 712: (Info -- Loss of precision (assignment) (long long to long))
main.cpp 430 error 747: (Info -- Significant prototype coercion (arg. no. 1) int to long long)
main.cpp 431 error 712: (Info -- Loss of precision (assignment) (long long to long))
main.cpp 434 error 1762: (Info -- Member function 'Huffman::file_size1(void)' could be made const)
main.cpp 439 error 578: (Warning -- Declaration of symbol 'end' hides symbol 'Huffman::end' (line 76))
main.cpp 76 error 830: (Info -- Location cited in prior message)
main.cpp 440 error 712: (Info -- Loss of precision (assignment) (long long to long))
main.cpp 441 error 747: (Info -- Significant prototype coercion (arg. no. 1) int to long long)
main.cpp 442 error 712: (Info -- Loss of precision (assignment) (long long to long))
main.cpp 446 error 1762: (Info -- Member function 'Huffman::file_size2(void)' could be made const)
main.cpp 450 error 578: (Warning -- Declaration of symbol 'root' hides symbol 'Huffman::root' (line 73))
main.cpp 73 error 830: (Info -- Location cited in prior message)
main.cpp 456 error 818: (Info -- Pointer parameter 'root' (line 450) could be declared as pointing to const)
main.cpp 450 error 830: (Info -- Location cited in prior message)
main.cpp 458 error 578: (Warning -- Declaration of symbol 'root' hides symbol 'Huffman::root' (line 73))
main.cpp 73 error 830: (Info -- Location cited in prior message)
main.cpp 463 error 525: (Warning -- Negative indentation from line 459)
main.cpp 459 error 830: (Info -- Location cited in prior message)
main.cpp 472 error 818: (Info -- Pointer parameter 'root' (line 458) could be declared as pointing to const)
main.cpp 458 error 830: (Info -- Location cited in prior message)
main.cpp 491 error 712: (Info -- Loss of precision (assignment) (long long to long))
main.cpp 492 error 737: (Info -- Loss of sign in promotion from long to unsigned long)
main.cpp 493 error 747: (Info -- Significant prototype coercion (arg. no. 1) int to long long)
main.cpp 515 error 712: (Info -- Loss of precision (arg. no. 1) (long long to unsigned long))
main.cpp 515 error 747: (Info -- Significant prototype coercion (arg. no. 1) long long to unsigned long)
main.cpp 516 error 747: (Info -- Significant prototype coercion (arg. no. 1) int to long long)
main.cpp 517 error 712: (Info -- Loss of precision (arg. no. 2) (long long to long))
main.cpp 517 error 747: (Info -- Significant prototype coercion (arg. no. 2) long long to long)
main.cpp 520 error 712: (Info -- Loss of precision (arg. no. 2) (long long to long))
main.cpp 520 error 747: (Info -- Significant prototype coercion (arg. no. 2) long long to long)
main.cpp 548 error 734: (Info -- Loss of precision (initialization) (31 bits to 7 bits))
main.cpp 553 error 701: (Info -- Shift left of signed quantity (int))
main.cpp 553 error 734: (Info -- Loss of precision (assignment) (31 bits to 7 bits))
main.cpp 561 error 747: (Info -- Significant prototype coercion (arg. no. 1) int to long long)
main.cpp 575 error 530: (Warning -- Symbol 'choice' (line 571) not initialized)
main.cpp 571 error 830: (Info -- Location cited in prior message)
moein_soltani 0 Newbie Poster
Here are some things to consider looking into:
Can you say how you find this error?
restrictment 91 Posting Whiz in Training
Can you say how you find this error?
You find these errors by 'debugging' your code. I would recommend Visual C++, as it seems to be the easiest to use/most detailed in my personal opinion.
moein_soltani 0 Newbie Poster
Ok,tnx I didn't have Visual C++ , but I liked to use that!..
I change my code compeletely and it works rather well..
Tnx for all of your help.
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.