Question

An old parser relies on a FILE * to work. However, the Dropbox Sync API for iOS returns a NSFileHandle * instead of a FILE * as a file handle.

So I try to use fileDescriptor of a NSFileHandle:

- (NSFileHandle )readHandle:(DBError *)error

Returns a read-only file handle for the file. If the file is not cached then the method will block until the file is downloaded. Returns

A file handle if the file can be read, or nil if an error occurred.

and passed as a FILE *:

- (void)loadDBFile:(DBFile *)dbFile
{
    DBError *dbError;
    NSFileHandle *fileHandle = [dbFile readHandle:&dbError];
    if (fileHandle) {
         FILE *file = fileHandle.fileDescriptor;
         fseek(file, 0, SEEK_END); // EXE_BAD_ACCESS here!
         // ...
    }
}

However, there is an EXE_BAD_ACCESS at the fseek line.

Was it helpful?

Solution

fileDescriptor returns the underlying file descriptor, a simple integer, not a FILE object (which wraps a file descriptor, but isn't the same thing). Use fdopen() to obtain a FILE object from the file descriptor:

FILE *file = fdopen([filehandle fileDescriptor], "r"); // Create a read-only FILE object
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top