Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create empty file for writing?

Tags:

objective-c

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

like image 997
fuzzygoat Avatar asked Dec 10 '09 11:12

fuzzygoat


People also ask

How do you create a text file and write it?

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.

What is an empty text file?

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.


1 Answers

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];
like image 112
Jarret Hardie Avatar answered Sep 30 '22 13:09

Jarret Hardie