Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using file pointers in Objective-C for iOS

I am trying to use a library (cfitsio) in an iOS application that heavily depends on using a file pointer to open and maintain access to a data file. I have successfully built an Objective-C Mac app that does what I need to do on iOS. Cocoa touch has methods to load a file in to NSData but I don't see anyway to get a file pointer directly, probably because of the stricter privacy around the file system on iOS. Is there a way to get a file pointer directly or use NSData to make a file pointer for temporary use with the library?

The c function I would be using to open the file is declared below. The file pointer continues to be used in many of the other library functions.

int ffopen(fitsfile **fptr,      /* O - FITS file pointer                   */ 
           const char *name,     /* I - full name of file to open           */
           int mode,             /* I - 0 = open readonly; 1 = read/write   */
           int *status);          /* IO - error status                       */
like image 846
Dash Avatar asked Jan 08 '13 21:01

Dash


2 Answers

Try using the NSFileManager which creats a NSFileHandle when you open a file. This handle has a getter for the fileDescriptor:

Returns the file descriptor associated with the receiver.
- (int)fileDescriptor
Return Value
The POSIX file descriptor associated with the receiver.

see also NSFileHandle

like image 165
AlexWien Avatar answered Oct 04 '22 03:10

AlexWien


It seems this library declares a type "fitsfile" and uses pointers "fitsfile *".

The function ffopen looks very much like it opens the file and creates the fitsfile* which it returns using a fitsfile**. So you don't have to worry about these files at all, the library does it.

So you'd probably write something like

NSString* path = <Objective-C code to get a path>;
BOOL readOnly = <YES or NO, your choice>
fitsfile* fptr = NULL;
int fstatus = 0;
int fresult = ffopen (&fptr, path.UTF8String, (readonly ? 0 : 1), &fstatus);

Since this library wants the data stored in a file that has a path, the only way to do this is to store the data in a file and pass the path to that file to ffopen.

like image 31
gnasher729 Avatar answered Oct 04 '22 04:10

gnasher729