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
First, open the text file for writing (or append) using the open() function. Second, write to the text file using the write() or writelines() method. Third, close the file using the close() method.
Empty describes anything which contains nothing. Using a computer as an example, an empty file is one that has no data in it; it has 0 bytes and is in plain text format having no data kept in a file.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With