LRC Receiver
#include<stdio.h>
#include<fcntl.h>
struct frame
{
char *info;
}f;
int getBit(char c,int pos);
main()
{
int fd,i,j,file1,k;
int n=3,count=0;
if( ((fd=open("testfile",O_RDONLY))<0) || ((file1=open("output.txt",O_WRONLY))<0) )
{
perror("file open failed");
}
for(;;)
{
char ch;
f.info=(char *)malloc(sizeof(char)*(4));
if((k=read(fd,f.info,4))<=0)
break;
for(i=8;i>=1;i--)
{
count=0;
for(j=0;j<4;j++)
{
if(getBit(f.info[j],i)==0)
{
count++;
}
}
if(count%2!=0)
{
perror("Error detected");
}
}
printf("\nMsg is: %s ",f.info);
if(k=write(file1,f.info,3)<=0)
{
printf("Msg write failed");
}
free(f.info);
}
}
int getBit(char c,int pos)
{
int no=128;
no=no>>(pos-1);
if((c&no)>1)
return 1;
else
return 0;
}
LRC Sender
#include<stdio.h>
#include<fcntl.h>
struct frame
{
char *info;
}f;
void displayBits(char c);
int getBit(char c,int pos);
char setBit(char c,int pos);
char unsetBit(char c,int pos);
main()
{
int fd,file1,i,j,k,count=0;
if( ((fd=open("testfile",O_WRONLY))<0) || ((file1=open("input.txt",O_RDONLY))<0))
{
perror("File open failed");
}
for(;;)
{
f.info=(char *)malloc(sizeof(char)*4);
if((k=read(file1,f.info,3))<=0)
{
break;
}
for(i=8;i>=1;i--)
{
count=0;
for(j=0;j<3;j++)
{
if(getBit(f.info[j],i)==1)
count++;
}
if(count%2!=0)
{
f.info[3]=setBit(f.info[3],i);
}
else
{
f.info[3]=unsetBit(f.info[3],i);
}
}
printf("\n");
for(i=0;i<4;i++)
{
printf("\n");
if(i==3)
printf("--------------\n");
displayBits(f.info[i]);
}
if((k=write(fd,f.info,4))<=0)
{
perror("File write failed");
}
free(f.info);
}
}
void displayBits(char c)
{
int no=128,i;
for(i=0;i<8;i++)
{
if((c&no)>0)
{
printf("1");
}
else
{
printf("0");
}
no=no>>1;
}
}
int getBit(char c,int pos)
{
int no=128;
no=no>>(pos-1);
if((no&c)>0)
return 1;
else
return 0;
}
char setBit(char c,int pos)
{
int no=128;
no=no>>(pos-1);
c=c|no;
return c;
}
char unsetBit(char c,int pos)
{
int no=128;
no=no>>(pos-1);
no=~no;
return c&no;
}