On Thu, 27 May 2004, Eric wrote:
>
> Assume that disk space is not an issue [...]
> Assume that transportation to another OS may never occur.
> Are there any solid reasons to prefer text files over binary files?
>
> Some of the reasons I can think of are:
>
> -- should transportation to another OS become useful or needed,
> the text files would be far easier to work with
I would guess this is wrong, in general. Think of the difference
between a DOS/Win32 text file, a MacOS text file, and a *nix text
file (hint: linefeeds and carriage returns). Now think of the
difference between the same systems' binary files (hint: nothing).
There do exist many free tools to deal with line-ending troubles,
though, so this isn't really a disadvantage; just a counter to your
claim.
> -- tolerant of basic data type size changes (enumerated types have been
> known to change in size from one version of a compiler to the next)
It's about five minutes' work to write portable binary I/O functions
in most languages, if you're worried about the size of 'int' on your
next computer or something. Check out any file-format standard for
ideas, and Google "network byte order." If you're coming from a C
background, then you'll understand when I tell you that 'fwrite' should
never, ever be applied to anything but buffers of 'unsigned char'!
> -- if a file becomes corrupted, it would be easier to find and repair
> the problem potentially avoiding the annoying case of just
> throwing it out
Yes, definitely. Also, it's much easier to tell if text has been
corrupted in transmission --- it won't look like text anymore!
Binary always looks like binary; you need explicit checksums and
guards against corruption there. (Again, see file-format standards,
especially my favorite, the PNG image standard.)
> I would like to begin using XML for the storage of application
> preferences, but I need to convince others who are convinced that binary
> files are the superior method that text files really are the way to go.
One major advantage of plain text is that it can be sent over HTTP
and other Web protocols without "armoring." You can put plain text
in the body of a POST request, for example, where I doubt arbitrary
bytes would be accepted. (I dunno, though.)
Along the same lines, you can email your data files back and forth
in the body of an email message, rather than mucking about with
attachments.
The disadvantage is size; but you don't seem worried about that.
Another possible disadvantage would be that text is easily read and
reverse-engineered, if you're worried about that (e.g., proprietary
config files or savefiles for a game) --- but then you can always
encrypt whatever you don't want read immediately. [Whatever you
don't want read *ever*, you simply don't give to your users, because
they'll crack anything given enough time.]
HTH,
-Arthur