从C程序写入命名管道以在bash脚本中读取
我试图从一个C程序写入一个命名管道,并用bash脚本从该管道读取数据。我使用两个终端,首先运行c程序,然后运行bash脚本。bash脚本从管道中读取数据,因为一旦读取,C程序就会被解锁,但$line始终为空 打印到终端的唯一内容是“完成读取”。我不确定该值为什么为空 Bash脚本:从C程序写入命名管道以在bash脚本中读取,c,pipe,ipc,C,Pipe,Ipc,我试图从一个C程序写入一个命名管道,并用bash脚本从该管道读取数据。我使用两个终端,首先运行c程序,然后运行bash脚本。bash脚本从管道中读取数据,因为一旦读取,C程序就会被解锁,但$line始终为空 打印到终端的唯一内容是“完成读取”。我不确定该值为什么为空 Bash脚本: #!/bin/bash pipe=/tmp/myfifo if read line <$pipe; then echo $line fi echo "done reading&q
#!/bin/bash
pipe=/tmp/myfifo
if read line <$pipe; then
echo $line
fi
echo "done reading"
#/bin/bash
管道=/tmp/myfifo
如果要读取Bash脚本中的行,则需要在C程序中编写行。如果你改变<代码> char s1 [ 50 ] =“从C++程序中的hello”,这应该起作用;代码>到<代码> char s1(50)=“来自C++程序\n的hello”;<代码>(其中\n
是换行符)。这就是问题所在,谢谢!
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
void WritePipe()
{
int fd1;
char * myfifo = "/tmp/myfifo";
mkfifo(myfifo, 0666);
char s1[50] = "Hello From C++ Program";
fd1 = open(myfifo, O_WRONLY);
write(fd1, s1, strlen(s1) + 1);
close(fd1);
}
int main()
{
WritePipe();
return 0;
}