Bit Stuffing CS1305 Network Lab in C programming

To write a program to perform bit stuffing for the given input data.
ALGORITHM BIT STUFFING
STEP 1: Start the program.
STEP 2: Create two file pointers.
STEP 3: Open two files one for input and the other for output.
STEP 4: The data is stored in the input file “in.txt”.
STEP 5: If the data consists of five continuous ones then a zero is inserted.
STEP 6: Then the bit stuffed output is stored in the output file “out.txt”.
STEP7: The data and the output is displayed.
STEP 8: Stop the program.
Source code in c
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp1,*fp2;
char c,prev,next;
int i=1;
clrscr();
fp1=fopen(“in.txt”,”r”);
fseek(fp1,0,SEEK_SET);
fp2=fopen(“out.txt”,”+w”);
if((c=fgetc(fp1))!=EOF)
{Bit Stuffing CS1305 Network Lab in C programming
fputc(c,fp2);
prev=c;
}
do
{
next=fgetc(fp1);
if(prev==’1’&&next==’1’)
{
fputc(next,fp2);
i++;
}
else
{
fputc(next,fp2);
i=1;
}
if(i==5)
{
fputc(‘0’,fp2);
i=0;
}
else
prev=next;
}
while(next!=EOF);
fclose(fp1);
fclose(fp2);
fp1=fopen(“in.txt”,”r’);
printf(“THE INPUT FILES ARE ‘);
do
{
c=getc(fp1);
putchar(c);
}
while(c!=EOF);
fclose(fp1);
printf(“BIT STUFFED OUTPUT FILE IS’);
fp2=fopen(‘out.txt’,”r”);
do
{
c=getc(fp2);
putchar(c );
}
while(c!=EOF);
fclose(fp2);
}
Input Output CS1305 Network Lab
THE INFUT FILES ARE 11111111111
THE BIT STUFFED OUTPUT IS 1111101111101
IN.TXT
11111111111
OUT.TXT
1111101111101
RESULT Thus the program for bit stuffing is executed and the output is verified. Free C Lab programming Source code in C C++ CS1305 Network Lab.

Related post