Linux C 通过管道实现文件复制
编写一程序,实现程序a通过读取一个文件,把文件内容写入一个有名管道.然后程序b读取有名管道的内容,重新写为一个新的文件。...
编写一程序,实现程序a通过读取一个文件,把文件内容写入一个有名管道.然后程序b读取有名管道的内容,重新写为一个新的文件。
展开
1个回答
展开全部
#include"stdio.h"
#include"stdlib.h"
#include"unistd.h"
#include"sys/types.h"
#include"sys/stat.h"
#include"string.h"
#include"fcntl.h"
#include"errno.h"
#define FIFO1 "/tmp/fifo"
#define MAXLINE 100
void client1(int);
void client2(int);
int main(int argc,char **argv)
{
int writefd;
writefd=open(FIFO1,O_WRONLY,0);
client1(writefd);
client2(writefd);
close(writefd);
unlink(FIFO1);
exit(0);
}
void client1(int writefd1)//实现从文件写到管道,自己创建一个aa.txt文件
{
char buff[MAXLINE];
int fd;
memset(buff,0,sizeof(buff));
fd=open("aa.txt",O_CREAT|O_RDWR,S_IRWXU);
read(fd,buff,sizeof(buff));
write(writefd1,buff,sizeof(buff));
close(fd);
}
void client2(int writefd2)//)//实现从管道写到另一个文件
{
char buff[MAXLINE];
int fd;
fd=open("bb.txt",O_CREAT|O_RDWR,S_IRWXU);
read(writefd2,buff,sizeof(buff));
write(fd,buff,sizeof(buff));
close(fd);
}
我这里省略了许多判断,自己加哈
如有问题,在线交流
#include"stdlib.h"
#include"unistd.h"
#include"sys/types.h"
#include"sys/stat.h"
#include"string.h"
#include"fcntl.h"
#include"errno.h"
#define FIFO1 "/tmp/fifo"
#define MAXLINE 100
void client1(int);
void client2(int);
int main(int argc,char **argv)
{
int writefd;
writefd=open(FIFO1,O_WRONLY,0);
client1(writefd);
client2(writefd);
close(writefd);
unlink(FIFO1);
exit(0);
}
void client1(int writefd1)//实现从文件写到管道,自己创建一个aa.txt文件
{
char buff[MAXLINE];
int fd;
memset(buff,0,sizeof(buff));
fd=open("aa.txt",O_CREAT|O_RDWR,S_IRWXU);
read(fd,buff,sizeof(buff));
write(writefd1,buff,sizeof(buff));
close(fd);
}
void client2(int writefd2)//)//实现从管道写到另一个文件
{
char buff[MAXLINE];
int fd;
fd=open("bb.txt",O_CREAT|O_RDWR,S_IRWXU);
read(writefd2,buff,sizeof(buff));
write(fd,buff,sizeof(buff));
close(fd);
}
我这里省略了许多判断,自己加哈
如有问题,在线交流
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询