【发布时间】:2015-04-11 10:45:52
【问题描述】:
我正在尝试使用 C++ 创建一个命名管道并在 python 上读取它。这是我的代码:
const int MAX_BUF = 1024;
string wr_string = "Hi.";
char text[MAX_BUF] = "";
strcpy(text, wr_string.c_str());
int fd = open("/tmp/test", O_WRONLY); // Open the pipe
write(fd,text,MAX_BUF); // Write
close(fd); // Close the pipe - allow the read
我是这样读的:
import os
import time
pipe = open("/tmp/OKC_avgprice", "r")
line = pipe.read()
pipe.close()
print line
但是,每次我尝试阅读它时,我都会得到这样的结果:
Traceback (most recent call last):
File "ipc.py", line 4, in <module>
pipe = open("/tmp/test", "r")
IOError: [Errno 2] No such file or directory: '/tmp/test'
写管道的时候应该是自动创建的吧?那为什么找不到呢?
谢谢!
【问题讨论】:
-
您的错误和您给我们的代码不同。你能检查一下一切都和你运行的完全一样吗,否则很难解决问题。
-
那不是真正的管道,它只是一个普通的文件。
-
您在 C 中使用
mkfifo(3)创建了一个命名管道或 fifo。此外,您的 C"++" 代码正在向管道打印垃圾。 -
至于你的问题,你确实需要检查C++代码中的错误。
open调用真的成功了吗? -
@Lattyware 实际上不是...
标签: python c++ ipc named-pipes fifo