Can't delete a file created by mkstemp() on Mac OS X

Posted by splicer on Stack Overflow See other posts from Stack Overflow or by splicer
Published on 2010-03-31T20:05:04Z Indexed on 2010/03/31 21:53 UTC
Read the original article Hit count: 220

Filed under:
|
|
|

Apparently, NSFileManager is unable to delete files created by mkstemp(). Here's some test code to demonstrate this:

char pathCString[] = "/tmp/temp.XXXXXX";
int fileDescriptor = mkstemp(pathCString);
if (fileDescriptor == -1) {
    NSLog(@"mkstemp failed");
} else {
    close(fileDescriptor);
    NSURL *url = [NSURL URLWithString:[NSString stringWithCString:pathCString encoding:NSASCIIStringEncoding]];
    NSLog(@"URL: %@", url);
    NSError *error;
    if (![[NSFileManager defaultManager] removeItemAtURL:url error:&error]) {
        NSLog(@"could not delete file: %@", error);
    }
}

Here's what I see in the log when I run the above code:

URL: /tmp/temp.A7DsLW
could not delete file: Error Domain=NSCocoaErrorDomain Code=4 UserInfo=0x1001108a0 "The file “temp.A7DsLW” doesn’t exist."

I'm running this on Snow Leopard. Any ideas on why the problem is occurring and/or how to work around it?

Thanks!

© Stack Overflow or respective owner

Related posts about mkstemp

Related posts about nsfilemanager