写
#include <sys/types.h>
#include <sys/stat.h>
#include<string.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include <fcntl.h>
#include<string.h>
#define NAME "test.txt"
int main()
{
if(access(NAME,F_OK))
{
int res = mkfifo(NAME,0664);
if(res==-1)
{
perror("mkfifo\n");
return -1;
}
}
int fd = open(NAME,O_WRONLY);
char buf[100]={0};
while(1)
{
printf("请输入\n");
scanf("%s",buf);
write(fd,buf,strlen(buf));
if(strcmp(buf,"q")==0)
exit(0);
memset(buf,0,100);
}
}
读
#include <sys/types.h>
#include <sys/stat.h>
#include<string.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include <fcntl.h>
#include<string.h>
#define NAME "test.txt"
int main()
{
if(access(NAME,F_OK))
{
int res = mkfifo(NAME,0664);
if(res==-1)
{
perror("mkfifo\n");
return -1;
}
}
int fd = open(NAME,O_RDONLY);
char buf[100]={0};
while(1)
{
read(fd,buf,100);
if(strcmp(buf,"q")==0)
exit(0);
printf("读到内容:%s\n",buf);
memset(buf,0,100);
}
}