Write to fifo file

506 views Asked by At

I've created a file by using mkfifo /tmp/my.fifo. I now want to write chars into the file (with Objective C) to be able to grab them by tail -f /tmp/my.fifo. For some reason this does not work. The tail command only displays one character, and then stops output.

Objective C Code:

NSError *error = nil;

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

The NSLog does not output anything. So from Objective C perspective theres obviously no error.

1

There are 1 answers

0
gorootde On BEST ANSWER

Solution was simpler than I thought:

NSFileHandle *fileHandle=[NSFileHandle fileHandleForWritingAtPath:@"/tmp/my.fifo"];
[fileHandle seekToEndOfFile];
[fileHandle writeData:[buffer dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];