tags:

views:

155

answers:

1

I am looking to create an empty file so I can open it for writing using NSFileHandle, is this the correct way or am I missing some better method?

success = [fileManager createFileAtPath:dataFile_OUT contents:nil attributes:nil];

outFile = [NSFileHandle fileHandleForWritingAtPath:dataFile_OUT];

gary

+2  A: 

I don't see anything wrong with what you've got... sounds logical if you are planning on writing to the file in a stream.

If the size and availability of your data is such that you don't need to maintain an open channel to which you can stream data (which I imagine is not your case since you explicitly specified needing to create an empty file), you could eliminate the second line:

NSString *content = @"Put this in a file please.";
NSData *fileContents = [content dataUsingEncoding:NSUTF8StringEncoding];
[[NSFileManager defaultManager] createFileAtPath:@"/Some/Path/foo.txt"
                                contents:fileContents
                                attributes:nil];
Jarret Hardie