Bit Stuffing Character stuffing using C

This is a program on Bit stuffing and Character stuffing using C language

This program is used for Networking.
In this program it includes both bit and character Stuffing and Unstuffing.

Here is the program

The Total Program code will not be given here to Download the total file 
click here

#include
#include
#include
main()
{
int ch;
clrscr();
do
{
printf("\n");
printf("\n 1:bit stuffing \n 2:character stuffing \n 3.exit \n Enter u r choice \n");
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("\n ERROR IN OPENING THE FILE");
exit(0);
}
printf("\n enter 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("\n DATA AFTER STUFFING \n");
while((ch=getc(fp))!=EOF)
{
putc(ch,stdout);
}
fseek(fp,0L,0);
printf("\n DATA 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 inpur.txt file opening problem.....");
exit(0);
}
printf("\n enter 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("\n input.txt file opening problem.....");
exit(0);
}
if((fp1=fopen("csource.txt","w"))==NULL)
{
printf("\n csource.txt file opening problem.....");
exit(0);
}
fputs(" DLE STX ",fp1);
printf("\n");
printf("\n DATA AFTER STUFFING \n");
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("\n source.txt file opening problem.....");
exit(0);
}
if((fp1=fopen("cdest.txt","w"))==NULL)
{
printf("\n cdest.txt file opening problem.....");
exit(0);
}
beg=ftell(fp);
beg+=9;
fseek(fp,-0L,2);
end=ftell(fp);
end-=9;
fclose(fp);
fp=fopen("csource.txt","r");
while((ch=getc(fp))!=EOF)
putc(ch,stdout);
fclose(fp);
printf("\n");
printf("\n THE DATA AFTER UNSTUFFING \n");
fp=fopen("csource.txt","r");
fgets(j,9,fp);
while(beg



Output





1 comments:

  1. Anonymous said...:

    thanx a lot...

Post a Comment