C++DAY32

C++DAY32_第1张图片

#include "head.h"
int main(int argc, const char *argv[])
{
	if(mkfifo("./fifo1",0664) < 0)
	{
		if(errno != 17)//文件已存在导致的错误是合法错误 17 == EEXIST
		{
			perror("mkfifo1");
			return -1;
		}
	}
	printf("mkfifo1 success\n");
	int f1 = open("./fifo1",O_WRONLY);
	if(f1 < 0)
	{
		perror("open");
		return -1;
	}
	printf("open success f1=%d\n",f1);
	int f2 = open("./fifo2",O_RDONLY);
	if(f2 < 0)
	{
		perror("open");
		return -1;
	}
	printf("open success f2=%d\n",f2);
	char bufA[128]="";
	ssize_t A = 0;
	while(1)
	{
		bzero(bufA,sizeof(bufA));//写操作
		printf("请输入--->");
		fgets(bufA,sizeof(bufA),stdin);
		bufA[strlen(bufA)-1] = 0;
		if(strcmp(bufA,"quit") == 0)
			break; 
		if(write(f1,bufA,sizeof(bufA)) < 0)
		{
			perror("write");
			return -1;
		}
		printf("发送成功\n");

		bzero(bufA,sizeof(bufA));//读操作
		A = read(f2,bufA,sizeof(bufA));
		if(A < 0)
		{
			perror("read");
			return -1;
		}
		else if(0 == A)
		{
			printf("数据读取完毕,且写端关闭\n");
			break;
		}
		printf("bufA=%s\n",bufA);
	}
	//关闭
	close(f1);
	close(f2);
	return 0;
}
#include "head.h"
int main(int argc, const char *argv[])
{
	if(mkfifo("./fifo2",0664) < 0)
	{
		if(errno != 17)//文件已存在导致的错误是合法错误 17 == EEXIST
		{
			perror("mkfifo2");
			return -1;
		}
	}
	printf("mkfifo2 success\n");
	int f1 = open("./fifo1",O_RDONLY);
	if(f1 < 0)
	{
		perror("open");
		return -1;
	}
	printf("open success f1=%d\n",f1);
	int f2 = open("./fifo2",O_WRONLY);
	if(f2 < 0)
	{
		perror("open");
		return -1;
	}
	printf("open success f2=%d\n",f2);
	char bufA[128]="";
	ssize_t A = 0;
	while(1)
	{	
		bzero(bufA,sizeof(bufA));//读操作
		A = read(f1,bufA,sizeof(bufA));
		if(A < 0)
		{
			perror("read");
			return -1;
		}
		else if(0 == A)
		{
			printf("数据读取完毕,且写端关闭\n");
			break;
		}
		printf("bufA=%s\n",bufA);

		bzero(bufA,sizeof(bufA));//写操作
		printf("请输入--->");
		fgets(bufA,sizeof(bufA),stdin);
		bufA[strlen(bufA)-1] = 0;
		if(strcmp(bufA,"quit") == 0)
			break;
		if(write(f2,bufA,sizeof(bufA)) < 0)
		{
			perror("write");
			return -1;
		}
		printf("发送成功\n");
	}
	//关闭
	close(f1);
	close(f2);
	return 0;
}

你可能感兴趣的:(c++,开发语言)