谁能提供一个c++管道使用实例
展开全部
#include<memory.h>
#define FIFO "myfifo"
main(){
int fd;
char buff[100];
if(access(FIFO,F_OK) == -1){
mkfifo(FIFO,0777);
}
fd=open(FIFO,O_RDONLY|O_NONBLOCK); //设置非阻塞打开,否则当没有输入时,会阻塞在read函数 </p> <p> int num;
while(1){
memset(buff,'\0',100); //如不清空最后的字符会出现乱码
if((num=read(fd,buff,100))==0){
printf("waiting.....\n");
sleep(1);
continue;
}
printf("read %d in fifo , it's %s",num,buff);
sleep(1);
}
}</p> <p>
fwrite.c //写文件</p> <p> </p> <p>#include<stdio.h>
#include<fcntl.h>
#include<memory.h>
#define FIFO "myfifo"
main(){
int fd;
char buff[100];
memset(buff,'\0',100);
scanf("%s",buff);
if(access(FIFO,F_OK) == -1){
mkfifo(FIFO,0777);
}
fd=open(FIFO,O_WRONLY);
int num;
num=write(fd,buff,strlen(buff));
printf("%d char is written! It's %s\n",num,buff);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询