Monday, 9 May 2016

Character Count program In C language

CCSender.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
char ch[500];
char ch1[500];int fd,i=1,k=0;
FILE *fp;
int c;
fd=open("mypipe",O_WRONLY);
fp=fopen("1.txt","r");ch1[0]='\0';
while(fgets(ch,500,fp)!=NULL)
{
sprintf(ch1,"%d#%s",strlen(ch),ch);
printf("%s",ch1);
write(fd,ch1,sizeof(ch1));
}
close(fd);
}






CCReceiver.c


main()
{
char ch[500],buf[200];int fd,i;
FILE *fp;
mkfifo("mypipe",0666);
fd=open("mypipe",O_RDONLY);
fp=fopen("2.txt","w+");
ch[0]='\0';
buf[0]='\0';
while(read(fd,ch,sizeof(ch)))
{
sscanf(ch,"%d#%[^\n]s",&i,buf);
strcat(buf,"\n");
printf("%d %s",i,buf);
if(i==(int)strlen(buf))
fputs(buf,fp);
else
{
printf("\nError In Frame Received...\n Resend Frames");
exit(0);
}
buf[0]='\0';
}
close(fd);unlink("mypipe");
}

No comments:

Post a Comment