DBF文件结构

来源:互联网 发布:西安源宇轩软件怎么样 编辑:程序博客网 时间:2024/06/04 17:52
A DBF file consists of a header record and data records. The header record defines the structure of the table and contains any other information related to the table. The header record starts at file position zero. Data records follow the header, in consecutive bytes, and contain the actual text of the fields.Note    The data in the data file starts at the position indicated in bytes 8 to 9 of the header record. Data records begin with a delete flag byte. If this byte is an ASCII space (0x20), the record is not deleted. If the first byte is an asterisk (0x2A), the record is deleted. The data from the fields named in the field subrecords follows the delete flag.

The length of a record, in bytes, is determined by summing the defined lengths of all fields. Integers in table files are stored with the least significant byte first.

DBF File Header

Byte offsetDescription0File type:
0x02    FoxBASE
0x03    FoxBASE+/Dbase III plus, no memo
0x30    Visual FoxPro
0x31    Visual FoxPro, autoincrement enabled
0x43    dBASE IV SQL table files, no memo
0x63    dBASE IV SQL system files, no memo
0x83    FoxBASE+/dBASE III PLUS, with memo
0x8B    dBASE IV with memo
0xCB    dBASE IV SQL table files, with memo
0xF5    FoxPro 2.x (or earlier) with memo
0xFB    FoxBASE1 - 3Last update (YYMMDD)4 – 7Number of records in file8 – 9Position of first data record10 – 11Length of one data record, including delete flag12 – 27Reserved28Table flags:
0x01    file has a structural .cdx
0x02    file has a Memo field
0x04    file is a database (.dbc)
This byte can contain the sum of any of the above values. For example, the value 0x03 indicates the table has a structural .cdx and a Memo field.29Code page mark30 – 31Reserved, contains 0x0032 – nField subrecords
The number of fields determines the number of field subrecords. One field subrecord exists for each field in the table.n+1Header record terminator (0x0D)n+2 to n+264A 263-byte range that contains the backlink, which is the relative path of an associated database (.dbc) file, information. If the first byte is 0x00, the file is not associated with a database. Therefore, database files always contain 0x00.

Field Subrecords Structure

Byte offsetDescription0 – 10Field name with a maximum of 10 characters. If less than 10, it is padded with null characters (0x00).11Field type:
C    –    Character
Y    –    Currency
N    –    Numeric
F    –    Float
D    –    Date
T    –    DateTime
B    –    Double
I    –    Integer
L    –    Logical
M    – Memo
G    – General
C    –    Character (binary)
M    –    Memo (binary)
P    –    Picture12 – 15Displacement of field in record16Length of field (in bytes)17Number of decimal places18Field flags:
0x01    System Column (not visible to user)
0x02    Column can store null values
0x04    Binary column (for CHAR and MEMO only)
0x06    (0x02+0x04) When a field is NULL and binary (Integer, Currency, and Character/Memo fields)
0x0C    Column is autoincrementing19 - 22Value of autoincrement Next value23Value of autoincrement Step value24 – 31Reserved

typedef struct
{
    FILE *fp;

    int         nRecords;

    int   nRecordLength;
    int   nHeaderLength;
    int   nFields;
    int   *panFieldOffset;
    int   *panFieldSize;
    int   *panFieldDecimals;
    char *pachFieldType;

    char *pszHeader;

    int   nCurrentRecord;
    int   bCurrentRecordModified;
    char *pszCurrentRecord;
   
    int   bNoHeader;
    int   bUpdated;
} DBFInfo;

-----------------------------------------------------------------------------------------------------------

DBF文件结构分为两大部分:文件结构说明区和数据区。  
一、文件结构说明区包括数据库参数区和记录结构表区。数据库参数区占32个字节:  
    
  1字节    数据库开始标志(若数据库含DBT文件为80H,否则为03H)  
  2-4字节  文件建立或修改的日期(YYMMDD 其中YY=日期-1900)  
  5-8字节  数据库的记录记录数,低字节在前,高字节在后  
  9-10字节  文件结构说明区长度  
  11-12字节  每条记录的总长度  
  12-32字节  保留  
   
记录结构表区包括各字段参数,每个字段占32字节:  
  1-11字节  字段名  
  12      字段类型  
  13-14   首记录中该字段对应内存地址的偏移量  
  15-16   首记录中该字段对应内存地址的段地址  
  17     字段长度  
  18     字段小数位数  
  在所有记录结构表区后是数据库结构结束标志,其中 Foxbase   以0D结束,dBASE   以0D,00  
结束。  
二、数据库数据区每条记录按字段依次存放,没有分隔符,也没有终止符,每条记录以删除标志  
 20H开始,若该记录被删除,则该标志为2AH 即“*”。  
 数据库的最后一个字节为结束标志1AH。