Ya,it is a advanced concept.But easy to understand.
Pipe is half duplex.In c we can achieve the pipe by using the function pipe().It internally uses fork.I think you may aware of fork.It will create a new process.
Pipe() will return two descriptors.one descriptor fd[0] is for reading and another descriptor fd[1] for writing.So if you write in the write end and you can read that in another end.For your requirement you just read from the source file and write that data it in the fd[1] (write end) then read that in the read end then you just write the data in the destination file.
To know in detail refer : man pipe .
I tried the requirement
Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include<fcntl.h>
#include<stdlib.h>
#define MAXSIZE 1024
main()
{
int fd[2];
pid_t childpid;
int f_desc,f_desc1,n;
char buf[MAXSIZE];
pipe(fd);
if((childpid = fork()) == -1)
{
perror("fork");
exit(1);
}
if(childpid == 0)
{
close(fd[1]);
if((n=read(fd[0],buf,MAXSIZE))!=-1)
{
if((f_desc1=open("backup",O_CREAT|O_RDWR,0644))!=1)
{
write(f_desc1,buf,n);
}
else
{
printf("open : error \n");
}
}
else
{
printf("read error");
}
}
else
{
close(fd[0]);
if((f_desc=open("a.c",O_RDONLY))!=-1)
{
if((n=read(f_desc,buf,MAXSIZE))!=-1)
{
write(fd[1],buf,n);
}
else
{
printf("read error");
}
}
else
{
printf("open : error \n");
}
}
}