Implementation of Text Compression [CS1255 - Graphics and Multimedia Lab]


AIM:
             To write a "C++" program for the Implementation of Text Compression in CS1255 - Graphics and Multimedia Lab.

SOURCE CODE:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
   char a[50],b[50],dl;
   int i,j,flag=0,countindex=0,index=-1,count=0,size;
   FILE *ip,*op;
   clrscr();
   printf("\n\n Enter the text for input:");
   scanf("%s",a);
   size=strlen(a);
   ip=fopen("source","w");
   fprintf(ip,"%s",a);
   printf("\n Input file length:");
   printf("%d",ftell(ip));
   fclose(ip);
   printf("\n");
   ip=fopen("compress.dat","w");
   for(i=0;i<size;i++)
   {
    j=i+1;
    if(a[j]==a[i])
      count=count+1;
    else
      if(count>0)
      {
    b[++index]=a[i];
    fprintf(ip,"%c",b[index]);
    b[++index]='#';
    fprintf(ip,"%c",b[index]);
    b[++index]=count+1;
    fprintf(ip,"%c",b[index]);
    count=0;
      }
    else
    {
     b[++index]=a[i];
     fprintf(ip,"%c",b[index]);
    }
    countindex=index;
   }
   fclose(ip);
   op=fopen("compress.dat","r+");
   printf("\n\n Compressed output\n ");
   for(i=0;i<size;i++)
   {
    fscanf(op,"%c",b[i]);
    if(b[i]=='#')
    {
     flag=1;
     printf("%c",b[i]);
     continue;
    }
    else if(flag==1)
    {
     printf("%d",b[i]);
     flag=0;
    }
    else
     printf("%c",b[i]);
   }
   printf("\n Compressed file length:");
   printf("%d",ftell(op));
   fclose(op);
   flag=0;
   op=fopen("compress.dat","r+");
   ip=fopen("decompress.dat","w");
   printf("\n\n\n Decompressed output:\n ");
   for(i=0;i<=countindex;i++)
   {
    fscanf(op,"%c",b[i]);
    if(b[i]=='#')
     {
      flag=1;
      continue;
     }
    else if(flag==1)
     {
     for(j=0;j<b[i]-1;j++)
      {
       fprintf(ip,"%c",dl);
       printf("%c",dl);
      }
      flag=0;
     }
    else if(b[i]!='\0')
     {
      fprintf(ip,"%c",b[i]);
      printf("%c",b[i]);
      dl=b[i];
     }
   }
   printf("\n Decompressed Length:");
   printf("%d",ftell(ip));
   fcloseall();
   getch();
}

OUTPUT:

Previous
Next Post »

Still not found what you are looking for? Try again here.