1

我已经使用mkfifo /tmp/my.fifo. 我现在想将字符写入文件(使用 Objective C),以便能够通过tail -f /tmp/my.fifo. 由于某种原因,这不起作用。tail 命令只显示一个字符,然后停止输出。

目标 C 代码:

NSError *error = nil;

[buffer writeToFile:@"/tmp/my.fifo" atomically:YES encoding:NSUTF8StringEncoding error:&error];
if(error){
    NSLog(@"Fail: %@",[error localizedDescription]);
}

NSLog 不输出任何内容。所以从Objective C的角度来看,显然没有错误。

4

1 回答 1

2

解决方案比我想象的要简单:

NSFileHandle *fileHandle=[NSFileHandle fileHandleForWritingAtPath:@"/tmp/my.fifo"];
[fileHandle seekToEndOfFile];
[fileHandle writeData:[buffer dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];
于 2013-12-02T16:26:27.530 回答