EmbLogic's Blog

Multiple Data Compression Program Upto Code Length

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

int open_file(char *);
int creat_masterarray(int );
int find_loc(char , char *, int);
int code_length(int );
int open_file(char *filename)
{
int fd;
fd = open(filename,O_RDONLY);
if(fd<0)
{
perror(“open”);
goto OUT;
}
printf(“File Opened Successfully. fd = %d\n”,fd);
return fd;

OUT:   exit(EXIT_FAILURE);
}

int creat_masterarray(int fd)
{
char ch,*master;
int count,flag=0,loc;

while(1)
{
count = read(fd,&ch,1);

if(count<0)
{
perror(“read”);
goto OUT;
}

if(ch == 10)
{
break;
}

if(flag==0)
{
master=(char *)malloc(1);
*master=ch;
flag++;
}
else
{
master=realloc(master,flag+1);
loc = find_loc(ch,master,flag);
if(loc)
{

*(master+flag)=ch;
flag++;
}
}

}

printf(“master array = %s”,master);
return flag;

OUT:
exit(EXIT_FAILURE);
}

int find_loc(char ch,char *ma,int size)
{
int i;
for(i=0;i<size;i++)
{
if( ch == *(ma+i))
return 0;
}

return i;
}
int code_length(int ndc)
{
if(ndc<=3)
return 2;
else if(ndc<=7)
return 3;
else if(ndc<=15)
return 4;
else if(ndc<=31)
return 5;
else if(ndc<=63)
return 6;
else if(ndc<=127)
return 7;
else
;
}

int main(int argc,char *argv[])
{
int fd,ndc,cl;

fd = open_file(argv[1]);
ndc = creat_masterarray(fd);
cl = code_length(ndc);

printf(“\nNDC  = %d”,ndc);
printf(“\ncode length  = %d”,cl);

return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>