NSString *tempFileTemplate =
[NSTemporaryDirectory() stringByAppendingPathComponent:@”myapptempfile.XXXXXX”];
const char *tempFileTemplateCString =
[tempFileTemplate fileSystemRepresentation];
char *tempFileNameCString = (char *)malloc(strlen(tempFileTemplateCString) + 1);
strcpy(tempFileNameCString, tempFileTemplateCString);
int fileDescriptor = mkstemp(tempFileNameCString);

if (fileDescriptor == -1)
{
// handle file creation failure
}

// This is the file name if you need to access the file by name, otherwise you can remove
// this line.
tempFileName =
[NSString
stringWithFileSystemRepresentation:tempFileNameCString
length:strlen(tempFileNameCString)];

free(tempFileNameCString);
tempFileHandle =
[[NSFileHandle alloc]
initWithFileDescriptor:fileDescriptor
closeOnDealloc:NO];

Advertisements