/*IMPLEMENTATION OF BIT STUFFING AND CHARACTER STUFFING*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
int ch;
clrscr();
do
{
printf("\n********************:");
printf("\n1.BIT STUFFING:");
printf("\n2.CHARACTER STUFFING:");
printf("\n3.EXIT:");
printf("\n*********************");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:bit();
break;
case 2:character();
break;
case 3:exit(0);
}
}while(ch!=3);
getch();
}
bit()
{
FILE *fp,*fp1;
char ch;
int i;
if((fp=fopen("source.txt","w"))==NULL)
{
printf("\nError in opening the file");
exit(0);
}
printf("\nEnter data to send press 'e' to end :\n");
i=0;
while(1)
{
scanf("%c",&ch);
if(ch=='e')
break;
if(ch=='1')
i++;
else
i=0;
putc(ch,fp);
if(i==5)
{
putc('0',fp);
i=0;
}
}
fclose(fp);
i=0;
fp=fopen("source.txt","r");
fp1=fopen("dest.txt","w");
printf("\nDATA AFTER STUFFING\n");
while((ch=getc(fp))!=EOF)
{
putc(ch,stdout);
}
fseek(fp,0L,0);
printf("\nDATA AFTER UNSTUFFING\n");
while((ch=getc(fp))!=EOF)
{
if(ch=='1')
i++;
else
i=0;
if(i==5)
{
putc(ch,stdout);
putc(ch,fp1);
ch=getc(fp);
i=0;
}
else
{
putc(ch,stdout);
putc(ch,fp1);
}
}
fclose(fp);
fclose(fp1);
}
character()
{
FILE *fp,*fp1;
char ch,c[2],k[4],j[9];
long beg,end;
if((fp=fopen("Input.txt","w"))==NULL)
{
printf("\n Input.txt file opening problem...");
exit(0);
}
printf("\nEnter data to send at end put '}': \n\n");
while(1)
{
scanf("%c",&ch);
if(ch=='}')
break;
putc(ch,fp);
}
fclose(fp);
if((fp=fopen("Input.txt","r"))==NULL)
{
printf("\nInput.txt file opening problem...");
exit(0);
}
if((fp1=fopen("csource.txt","w"))==NULL)
{
printf("\ncsource.txt file opening problem...");
exit(0);
}
fputs(" DLE STX ",fp1);
while((ch=getc(fp))!=EOF)
{
if(ch=='D')
{
c[0]=getc(fp);
c[1]=getc(fp);
if(c[0]=='L'&&c[1]=='E')
fputs("DLE",fp1);
putc(ch,fp1);
putc(c[0],fp1);
putc(c[1],fp1);
}
else
putc(ch,fp1);
}
fputs(" DLE ETX ",fp1);
fclose(fp);
fclose(fp1);
if((fp=fopen("csource.txt","r"))==NULL)
{
printf("\ncsource.txt file opening problem...");
exit(0);
}
if((fp1=fopen("cdest.txt","w"))==NULL)
{
printf("\ncdest.txt file opening problem...");
exit(0);
}
beg=ftell(fp);
beg+=9;
fseek(fp,-0L,2);
end=ftell(fp);
end-=9;
fclose(fp);
printf("\nData after stuffing ");
fp=fopen("csource.txt","r");
while((ch=getc(fp))!=EOF)
putc(ch,stdout);
fclose(fp);
printf("\n");
printf("\nThe data after destuffing");
fp=fopen("csource.txt","r");
fgets(j,9,fp);
while(beg<=end)
{
ch=getc(fp);
if(ch=='D')
{
c[0]=getc(fp);
c[1]=getc(fp);
if(c[0]=='L'&&c[1]=='E')
{
fgets(k,4,fp);
beg+=4;
}
else
{
putc(ch,fp1);
putc(c[0],fp1);
putc(c[1],fp1);
putc(ch,stdout);
putc(c[0],stdout);
putc(c[1],stdout);
}
}
else
{
putc(ch,fp1);
putc(ch,stdout);
}
beg++;
}
fclose(fp);
fclose(fp1);
}
OUTPUT
********************:
1.BIT STUFFING:
2.CHARACTER STUFFING:
3.EXIT:
*********************
Enter your choice:1
Enter data to send press 'e' to end :
11100000111111111101e
DATA AFTER STUFFING
1110000011111011111001
DATA AFTER UNSTUFFING
11100000111111111101
********************:
1.BIT STUFFING:
2.CHARACTER STUFFING:
3.EXIT:
*********************
Enter your choice:2
Enter data to send at end put '}':
This is DLE idle DLE program}
Data after stuffing
DLE STX This is DLEDLE idle DLEDLE program DLE ETX
The data after destuffing
This is idle program
********************:
1.BIT STUFFING:
2.CHARACTER STUFFING:
3.EXIT:
*********************
Enter your choice:3