Take the 2-minute tour ×

Are there any advantages to a fixed-width file format over something like XML? I realize XML would likely take up more disk space to store the same amount of data but the file could also be compressed. I guess you could also, in theory, read a specific piece of data based on where it is in the file (just grab those bytes). But other than that, what else?

share|improve this question
File formats for what? Do you want images or video as compressed XML? –  svick Oct 5 '11 at 19:49
@svick: don't be a fool, you know what I'm asking! –  Josh M. Oct 5 '11 at 20:09
No, I really don't. I don't know what kind of file formats are you talking about and it's not clear to me from your question. Maybe I just didn't encounter those formats. Could you specify that more explicitly? –  svick Oct 5 '11 at 20:19
I haven't seen a fixed-width file format for years. I thought they died out with punched cards. They're very efficient of course, but have zero potential for change. –  Michael Kay Oct 5 '11 at 21:30
I have seem then for well logs (drilling), and for geological information, such as CanStrat. Usually, these are legacy files, and may be coming from/going to equipment that just hasn't been updated in a long time. Sometimes, these files are also printed out on dot-matrix printers and the fixed width ensure that it stays in the width limit (ie: 72 characters). –  Sheldon Warkentin Jan 27 '12 at 22:03
add comment

4 Answers

When the data is large (Giga/Terra-bytes), fixed width format files can be MUCH more efficient.

Since each record and field has fixed sizes, you can simply seek to the (for example) n-millionth row and read a couple of records from there. You can also memory map the whole file into memory and get rather efficient and easy random access to everything.

XML files aren't a good fit in these cases.

share|improve this answer
add comment

XML is complicated. And especially if you do validation according to a schema. This may not look important, because somebody else already wrote XML parser that you can use. But this adds quite a lot of processing, which means it takes longer. This may not be a problem in many cases, but sometimes can.

If you want to save one integer into a custom file format, it takes just 4 bytes and when you want to load it, you just copy those 4 bytes into memory (assuming the file format and your platform have the same endianness). But with XML, it might take something like 10–30 bytes. And loading it is means comparing strings and parsing decimal representations of integers and probably more.

Again, those performance and storage size differences may very well be too minuscule for you to even consider (and the work that it would take to devise custom format might be non-trivial), but in many cases, those differences do matter.

For example, I encountered a system that uses SMS messages for transmission of some data. That means you have 140 bytes (!) per message. And the device that sends and recieves those messages doesn't have GBs of memory and GHz of CPU. In that situation, you make sure that every bit counts and you certainly don't use XML.

share|improve this answer
Thanks for the answer. However, I'd argue that a complex fixed-width file is more confusing than a complex XML file. At least you can read the XML file! –  Josh M. Oct 6 '11 at 1:24
I still have no idea what kind of fixed-width formats are you talking about, so I can't respond to that. –  svick Oct 6 '11 at 11:05
add comment

Probably mostly for legacy reasons, since parsers for XML, JSON (etc) exist pretty much on all platforms.

Theoretically fixed-width formats can be more space-efficient, as you suggest; and reading bit simpler. But these do not seem like significant benefits.

For what it is worth, tabular (but not fixed-width) formats like CSV have their uses, combining bit more compact representation and possibly better readability; CSV works quite nicely for map/reduce style jobs.

share|improve this answer
add comment

One reason could be that processing XML (not just reading and loading into memory structures, but think about regex searching in an XML file vs. a simple fixed-width or delimited file, or even making manual quick-fixes to bad data) is more complicated than fixed-width files. Sure, there are many libraries that can do it for you now, but if there isn't one for the platform you're working on, do you really want to write an XML parser, or a program that just reads n bytes at location x?

share|improve this answer
add comment

Your Answer


