Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
With Cocoa, every time I try to create a file that is empty (with no characters in it) it creates a zero-kbyte file which when accessed will crash my program. I create this plain text file with the "- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag" function. I call it with an empty nsstring (@"") as an argument.

Saving an empty plain text file using textedit results in a 4kbyte file. How can I make that happen using Cocoa?
 

iSee

macrumors 68040
Oct 25, 2004
3,539
272
It can be correct for an empty plain text file to be zero bytes in size. The fix, I think, is to change your program so it can handle zero byte files.

It actually depends on the encoding used for the file. For ASCII or UTF-8 (with no byte-order mark) encoded files, a size of zero is correct for an empty document.

TextEdit might be storing something in the file's resource fork, which would show up in the file's total size. If you haven't encountered this before, under Mac OS, files can have a data fork--the "normal" part of the file--and a resource fork--which can be used for any purpose, but typically stores meta data or other related resources in a particular format. A text editor, for example, might save where your edit cursor and scroll position in a document was when you last edited it. That way, it could restore your view on the file when you open it back up. Resource forks are used a lot less in OS X than they were in Mac OS Classic but are still handy for some things.
 

ChrisA

macrumors G5
Jan 5, 2006
12,578
1,695
Redondo Beach, California
Saving an empty plain text file using textedit results in a 4kbyte file. How can I make that happen using Cocoa?

Have you tried writing an end of file character (control-D) in the first byte of the file?

If you want to make an empty file fro the command line, that is what "touch" is for. see "man touch" for details but all yu type is "touch myfilename" and you get a zero length file.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Maybe you should show us the code that crashes when you access the file, not the code that creates the file? An empty file is perfectly valid. Unfortunately if you really do want to create a file full of nulls I am not sure how to do this with Cocoa. I know you can do it using dd from /dev/zero on most BSD and Linux-like systems, and tools like prealloc on HP-UX from the command-line, but don't know the Cocoa API well enough to tell you. You could do a write of a few K of null characters.

I'm betting if you save a blank file from TextEdit it is still putting RTF headers and things on the file. You can see what's in there using something like:
od -ta myfile.rtf
from the commandline, substituting the name of the file you saved from textedit for myfile.rtf.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.