I have a block of code
if ( [fm fileExistsAtPath: userLogTextFile] ) {
NSFileHandle * file;
file = [NSFileHandle fileHandleForUpdatingAtPath: userLogTextFile];
NSData * dataBuffer = [NSData dataWithBytes: [outputLine UTF8String] length: [outputLine length]];
if ( nil == file ) {
NSLog(@"Failed to open file");
NSLog ( @"Failed to create file Handle or write to log file: %@, %@", userLogTextFile, NSStringFromSelector( _cmd ) );
NSLog ( @"%@", outputLine );
NSLog ( @"Error: %@", error.description );
return;
}
[file seekToEndOfFile];
[file writeData: dataBuffer];
[file closeFile];
} else {
NSLog ( @"Log file does not exist: %@", userLogTextFile );
}
On execution the FileHandle is instantiated but "file" is alkways nil after line 3.k
As a side note why is there no append to file method? Leaving large files in memory is a nice way tp sell memory but really
Any advice is grateefully received as to how to solve this problem.
ClarkW
You don't need both dispatch_io_create and dispatch_io_create_with_path. One or the other. The first is when you already have a file descriptor, the second when you have a path.
>>figure out how to set file handle to the end to append data
The dispatch_io_write function has an offset parameter. The documentation says this is relative to the file position set when you called dispatch_io_create_with_path, if you open with "random" io_type, or ignored if you open with "stream".
So, using the correct oflag and mode parameters to cause the file to opened for appending, and try "stream" first. If you're lucky, a single dispatch_io_write will be all you need, and you won't even need to know the existing file length.
>> have gotten obscenly complicated
I dunno, you started with open, seek, write, close. With GCD you have open, write, close. The GCD C interface is a bit ugly, but you get asynchronicity for free, and you get progress monitoring for free.
As I said, GCD I/O has a small learning curve. If you truly hate the GCD approach, then it's OK to go back to NSFIleHandle. Just create your own file descriptor in advance, to find out what the open error was, and make sure you handle exceptions.