[R] Re: read dbf files into R

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Wed Sep 29 12:43:44 CEST 2004


On 29-Sep-04 Vikas Rawal wrote:
> 
> Is there a linux-based/free command line tool for converting
> dbf files into txt?

It might not be difficult to write one. You basically need to
first decipher the file header, after which reading in the
database records is straightfoward.

DBF file structure (byte counts start at 0):

    0: dBASE version number
 1- 3: Date of last update (YY MM DD as 3 separate BCD bytes)
 4- 7: (binary int) Number of records in file
 8- 9: (binary int) Total number of bytes in header (incl final "0Dh")
10-11: (binary int) Number of bytes per record
12-31: Reserved (not needed for reading file)
32-**: Series of 32-byte descriptions, one for each field
Last : "Carriage-return" byte (hex "0D")

For each 32-byte field descriptor:

 0-10: Name of field (padded with zero "00" bytes)
   11: Field type (ASCII letter: C N L D or M)
12-15: Field RAM address (not relevant for reading file)
   16: (binary int) Length of field in bytes
   17: (binary int) Number of decimal places
18-31: Not usefully informative for reading file

So if there are N fields, the header will occupy 32*(N+1)+1 bytes.

Thereafter, you can work out the length of each record in bytes
from the info in the field descriptors; each record starts with
an additional byte which is "*" if the record is marked for
deletion, otherwise " " (space). There is no delimiter at the
end of a field, nor at the end of a record (so use simply byte
counts).

Then read in that number of bytes, dissect it into fields (according
to field lengths), and output the contents (e.g. comma-delimited)
into one line of the destination. Repeat until no more records remain.

All info in fields is stored as ASCII-coded characters, so can
be written straight out once read in.

Note that Logical data (always 1 byte) may be
"?" "Y", "y", "N", "n", "T", "t", "F", "f"

and Date data are YYYYMMDD

(I'm assuming that there are no "Memo" data, which are not stored
in the DBF file but in a separate DBT file).

Hope this helps,
Ted.


--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861  [NB: New number!]
Date: 29-Sep-04                                       Time: 11:43:44
------------------------------ XFMail ------------------------------




More information about the R-help mailing list