Hi all,
I'm trying to write the Hufmann code to encode a given file & give me the compression ratio. but when I run this programme, I'm getting segmentation fault. Can you please help me with correcting it ?

/*************************/
/*************************/
// HUFMANN.C
/*************************/
/*************************/

#include<stdio.h>
#include<stdlib.h>
#include<input.h>

#define MAXBITS 32
#define MAXNODES 512
#define MAXSYMBS 256

int hmin(void);
void hinsert(int,int);
struct codetype{
   int bits[MAXBITS];
   int startpos;
   };

struct nodetype{
   int freq;
   int father;
   int isleft;
   };

typedef struct hlist{
   int pos;
   int hfreq;
   struct hlist *next;
   }hnode;

hnode *hroot=NULL,*traversal;
extern struct list a[256];
extern int n;
int hmin()

{
 int p;
 p = hroot->pos;
 traversal=hroot;
 hroot = traversal->next;
 free(traversal);
 return(p);
}
void hinsert(int p,int freq)
{
  hnode* new1=(hnode *)malloc(sizeof(hnode));
  new1->pos = p;
  new1->hfreq = freq;
  traversal = hroot;
  if(hroot == NULL)
  {
        hroot = new1;
        hroot->next = NULL;
        return;
  }
  if(hroot->next == NULL)
  {
        if(hroot->hfreq>new1->hfreq)
        {
                new1->next = hroot;
                hroot =new1;
                traversal->next =NULL;
                return;
        }
        else
        {
           hroot->next =new1;
           new1->next =NULL;
           return;
        }
  }
  if(hroot->hfreq>=new1->hfreq)
  {
        new1->next =hroot;
        hroot = new1;
        return;
  }
 
  while(traversal->next->hfreq<new1->hfreq)
  {
        traversal=traversal->next;
        if(traversal->next==NULL)
        break;
  }
  if(traversal->next->hfreq>=new1->hfreq)
  {
        new1->next = traversal->next;
        traversal->next = new1;
        return;
  }
 
  new1->next = NULL;
  traversal->next = new1;
 
}
int main(){
  struct codetype cd,code[MAXSYMBS];
  struct nodetype node[MAXNODES];
  int i,k,p,p1,p2,root;
 
  char symb,alph[MAXSYMBS];
  system('clear');
  for(i=0;i<MAXSYMBS;i++)
  alph[i]=' ';
  //scanf("%d",&n);
  input();
  for(i=0;i<n;i++){
         fflush(stdin);
         scanf("%c %d",&symb,&node[i].freq);
        // symb = a[i].alph;
         //node[i].freq = a[i].freq;
         hinsert(i,node[i].freq);
 
         alph[i]=symb;
         }
 
  for(p=n;p<(2*n-1);p++){
 
         p1 =hmin();
 
         p2 =hmin();
 
         node[p1].father = p;
         node[p1].isleft = 1;
         node[p2].father = p;
         node[p2].isleft = 0;
         node[p].freq =node[p1].freq+node[p2].freq;
         hinsert(p,node[p].freq);
  }
         root = hmin();
  for(i=0;i<n;i++){
         cd.startpos = MAXBITS;
         p=i;
         while(p!=root){
                --cd.startpos;
                if(node[p].isleft)
                   cd.bits[cd.startpos] =0;
                else
                   cd.bits[cd.startpos] =1;
                p =node[p].father;
         }
         for(k=cd.startpos;k<MAXBITS;k++)
                code[i].bits[k]=cd.bits[k];
         code[i].startpos =cd.startpos;
  }
  for(i=0;i<n;i++){
         printf("\n%c  %d",alph[i],node[i].freq);
         for(k=code[i].startpos;k<MAXBITS;k++)
                printf(" %d",code[i].bits[k]);
         printf("\n");
         }
         return(0);
  }
 

/*************************/
// INPUT.H
/*************************/
 
 #include<stdio.h>
struct list{
char alph;
int freq;
};
struct list a[256];
int n;
input()
{
FILE *fin,*fout;
char *filein,*fileout,ch;
int i,k,f;
 
printf("enter the filename of from which the data is to be read::\n");
scanf("%s",filein);
fin=fopen(filein,"r");
for(i=0,n=0;i<256;i++)
{        f=0;k=0;
  while((ch=fgetc(fin))!=EOF)
  {
         if(ch==i)
         { f=1;
         a[n].alph=ch;
         k++;
         }
         }
         if(f==1){
         a[n].freq =k;
         n++;
        }
 
         rewind(fin);
}
 
fclose(fin);

}

Nobody knows your program as well as you do. Don't be afraid to sharpen your own troubleshooting skills.

Narrow down what function first gets bad data (you can bet it's a memory error causing this). A few printf()'s in functions and stepping through the code should do wonders.

And you can do that - and should, imo. How will you learn to troubleshoot your programs if you don't practice? Like here, you didn't mention what function is causing the crash. Why not? Don't be lazy, try to find the problem.

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.