管道没有这样的文件或目录
Pipe no such file or directory
我正试图用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):创建命名管道
mkfifo("/tmp/test", 0600) // 0600 means writable and readable by owner only
这样的fifo将作为出现在ls -laF
(GNU)上
prw------- 1 user group 0 Apr 12 07:02 test|
值得注意的是,该行将以p
开头,并且名称后面将有一个|
。管道将保留在磁盘上yes(尽管/tmp
通常在重新启动时清空)。
请注意,如果尝试使用O_WRONLY
打开文件,但文件不存在,则open
将使用ENOENT
失败,返回-1
作为fd
。CCD_ 11将永远不会尝试创建仅具有CCD_ 12的新文件;要创建一个新的常规文件,您需要调用
open("/tmp/test", O_WRONLY|O_CREAT, 0600);
其中0600是文件所需的模式/权限。
用-1
调用write
和close
随后将用EBADF
失败请理解您必须始终检查所有系统调用的返回值。有时在Stackoverflow示例中,为了简洁起见,它们被省略了,应该知道需要添加检查。
要写入C++string
的内容,请直接从.c_str()
:写入
write(fd, wr_string.c_str(), wr_string.length());
还要始终检查来自C函数的错误代码;mkfifo
、open
和write
可能会失败,并且它们返回值< 0
,您需要做好处理这些情况的准备。
相关文章:
- 如何使用管道在父级和子级之间来回传递文件
- GLib-ERROR:为GWakeup创建管道:打开的文件太多
- 如何在 bash 中使用管道在 c++ 中使用 getenv() 访问文件?
- 使用命名管道块的读取文件,尽管使用重叠
- 确定文件句柄是否为管道句柄
- 使用 Select 多路复用未命名的管道和其他文件描述符
- 如何将管道作为文件传递到C应用程序
- read() 上的不同行为取决于写入不可写内存时表示文件、匿名管道或套接字的文件描述符
- 读取文件挂在管道读取上
- C++ Windows 管道写入文件到变量?
- 命名管道,选择错误的文件描述符C++
- 检查文件是否是C++中的命名管道 (fifo)
- 管道没有这样的文件或目录
- 使用外部可执行文件作为管道的Qt 'way'是什么?
- 如何检查文件指针/描述符/句柄是否关联到同一个文件/管道/终端
- C++管道,打开的文件太多,错误25
- 管道损坏,FIFO 文件
- 命名管道:连接命名管道返回ERROR_BROKEN_PIPE后的读取文件
- C++ 在关键字段上联接两个管道分隔文件
- 如何将 gdb 的完整堆栈跟踪通过管道传输到文件?