/* fifo_read.c 不变,只修改 fifo_write.c*/
/* fifo_write.c */
#include<unistd.h> //write,read,close,access
#include<stdio.h> //printf,sscanf,
#include<string.h> //memset,
#include<limits.h> // PIPE_BUF
#include<fcntl.h> //open,O_RDONLY....;
#include<stdlib.h> //exit,
#define MYFIFO "/tmp/myfifo"
#define MAX_BUFFER_SIZE PIPE_BUF
int main(int argc, char *argv[])
{
int fd;
int nwrite;
char buff[MAX_BUFFER_SIZE];
if(argc <= 1)
{
printf("Usage: ./fifo_write string\n");
exit(1);
}
sscanf(argv[1],"%s",buff);
/**************************************************************************/
/* 添加部分,通过 strcat 函数将 argv[1]、argv[2]、argv[3] 等以及空格拼接起来。*/
if(argc > 2)
{
for(i=2; i<argc; i++)
{
strcat(buff,kongge);
strcat(buff,argv[i]);
}
}
/**************************************************************************/
fd = open(MYFIFO,O_WRONLY);
if(fd == -1)
{
printf("Open fifo file error\n");
exit(1);
}
nwrite = write(fd,buff,MAX_BUFFER_SIZE);
if(nwrite > 0)
{
printf("Write '%s' to FIFO\n", buff);
}
close(fd);
exit(0);
}
转载于:https://my.oschina.net/fengyeshangqing/blog/335819