Creating an empty non-zero kbytes text file

Discussion in 'Mac Programming' started by Soulstorm, Apr 29, 2008.

  1. Soulstorm macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #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?
     
  2. iSee macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #2
    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.
     
  3. ChrisA macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #3
    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.
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    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
     

Share This Page