gpt4 book ai didi

objective-c - 访问命名管道以进行读写

转载 作者:行者123 更新时间:2023-12-03 17:18:54 27 4
gpt4 key购买 nike

我正在尝试创建一个简单的工具,可以创建、写入和读取命名管道。这是代码。

int main(int argc, const char * argv[])
{
@autoreleasepool
{
//create pipe
system("rm /tmp/myPipe");
system("mkfifo /tmp/myPipe");
system("chmod 666 /tmp/myPipe");

// write to pipe
NSString *text = [NSString stringWithFormat:@"this is a test"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:@"/tmp/myPipe" isDirectory:NO])
{
NSFileHandle *fileHandle=[NSFileHandle fileHandleForWritingAtPath:@"/tmp/myPipe"];
[fileHandle seekToEndOfFile];
[fileHandle writeData:[text dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];

}
}
return 0;
}

文件已正确创建,权限为prw-rw-rw- 1 xxx 轮 0 5 月 17 日 09:48 myPipe|

当我运行该工具时,它卡在以下行:

NSFileHandle *fileHandle=[NSFileHandle fileHandleForWritingAtPath:@"/tmp/myPipe"];

我在这里缺少什么?

最佳答案

除了我完全不了解代码的作用之外,这里没有任何问题。没有“挂起”,只是代码在管道中等待外部消息。

我只需要启动终端并输入

echo "foo" > /tmp/myPipe

感谢所有帮助过的人...

关于objective-c - 访问命名管道以进行读写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44030859/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com