Binary File Format of DBF Files

Source: Internet
Author: User
Tags dbase

Binary File Format of. DBF Files

 

Table files consist of header records and data records. The header record defines the structure of the table and contains other table-related information. The header record starts with the file position 0. Data Record 1 is followed by the header record (consecutive bytes) and contains the actual text in the field.
The record length (in bytes) is equal to the sum of the length defined by all fields. When an integer is stored in a table file, the low byte is in front of the table file.
1. Structure of the table header record:
Byte offset description
0File Type
0x02 FoxBASE
0x03 FOXBASE +/dbase iii Plus, no remarks
0x30 Visual FoxPro
0x43 dbase iv SQL table file with no remarks
0x63 dbase iv SQL System File with no remarks
0x83 FOXBASE +/dbase iii Plus, with remarks
0x8b dbase iv remarks
0xcb dbase iv SQL table file with remarks
0xf5 FoxPro 2.x( or earlier) has remarks
0xfb FoxBASE

1-3Last Update Time (yymmdd)

4-7Number of records in the file

8-9Location of the first data record

10-11The length of each data record (including the delete tag)

12-27Retained

28Table tag

0x01 files with. CDX Structure
0x02 file contains remarks.
0x04 files are databases (. DBC)
Note that this byte can contain any sum of the upper nominal values. For example, 0x03 indicates that the table has a structured. CDX and a remarks field.

29Code Page mark

30-31Reserved, including 0x00

32-nField subrecord
The number of fields determines the number of Field subrecords.

Each field in the table corresponds to a field subrecord.
N + 1Header record terminator (0x0d ),
N + 2ToN + 264The 263 bytes in this range contain the post-link information (the relative path of the relevant database (. DBC ). If the first byte is 0x00, the file is not associated with the database. Therefore, the database file itself always contains 0x00.

The 8th to 9th bytes in the header record indicate the starting position of the data in the data file.

The data record starts from the mark 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.

After marking, it is the data in the fields named in the field record.

2. Field subrecord Structure

Byte offset description
0-10Field name (up to 10 characters-enter blank characters (0x00) if less than 10 characters)
11Field Type
C-memory type
Y-currency type
N-numeric type
F-floating point type
D-date type
T-Date and Time Type
B-Double Precision type
I-integer
L-logical type
M-remark type
G-General Type
C-bytes (Binary)
M-remarks (Binary)
P-image type

12-15Offset of the field in the record

16Field Length (in bytes)

17Decimal places

18Field tag

0x01 system column (invisible to Users)
0x02 columns that store null values
0x04 binary columns (applicable only to the comment and comment types)

19-32Retained

3. _ nullflags field description

In some DBF Files, a field named _ nullflags is found. What is this field used. After parsing, the field information is found as follows:

Field name: _ Nullflags
Field Type: 0 (Arabic digit 0)
Field Length: 2
Field tag: 0x05 (that is, the sum of 0x01 (system column) and 0x04 (Binary column)

The DBF structure does not allow variable-length fields. Therefore, the varchar and varbinary fields in the DBF table are actually filled with spaces. Therefore, Microsoft already has a mechanism to track the length of the varchar and varbinary fields to ensure that the correct trim value is returned when necessary.

According to relevant information, if a field in all versions of the DBF table of visualfoxpro can accept null values, a hidden field named _ nullflags will be added to the table. This field contains a bit value to indicate whether a specific field in a specified record contains a null value. For example, if the first field in a record that can receive null contains a null value, the bit 0 in _ nullflags is set to 1. If the second field that can receive null values contains a non-null value, the bit 1 in _ nullflags is set to 0. Because one byte has eight bits, _ nullflag
The length of a field in can receive null value fields is 8 (that is, the number of fields with a 18th-byte value of 0x02 in the field sub-record/8 + 1 and then rounded up, that is, the length of the _ nullflags field ).

In VFP 9, _ nullflags serves two responsibilities: its bit also indicates whether the value in the varchar and varbinary fields fills up the field. If a single digit contains 0, the length of the value in a field is equal to the length of the field (This field is full ). If this bit contains 1, the length of the value is smaller than the length of the field. In this case, the field is filled with spaces as needed, and the last bit contains the length of the field. For example, a 10-bit length varchar field containing "AB" actually contains seven spaces after "AB" and a CHR (2) (2 indicates the length of the value), at the same time in the _ nullsflags
The bit of this field in is 1.

If a field can receive null values of the varchar or varbinary type, two fields are used to represent a field. The low one represents the "full" state, and the high one represents the null state. For example, a 10-bit varchar field that can receive null contains "AB", which is represented by 01 in _ nullflags (0 means not null, 1 means not satisfied ), if a null value is in the same field, it is expressed by 11 (null and not satisfied ).

4. memo file structure (. FPT)

The remarks file contains a header record and any number of block structures. The header record contains the pointer pointing to the next free block and the block size in bytes. The block size is determined by the set blocksize command during file creation. The header record starts from 0 and occupies 512 bytes. Run the set blocksjze to 0 command to set the block size to 1.

The header record contains the block header and the remarks text. The table package contains the block number used to reference the remarks block. The position of a block in the remarks file can be obtained by multiplying the block number and the block size (created in the header record of the remarks file. All remark blocks start with an even block boundary address. Each remark block can occupy more than one continuous block.

Remarks header record

Byte offset description
00-03 location of the next free block1 *
04-05 not used
06-07 block size (number of bytes per block)1 *
08-511 not used
1 *When an integer is stored, the high byte comes first.
The "next free block" refers to the end of the file, because the remarks file can contain any number of block structures.
When adding records, you must note that you must increase the number in blocks. The default size is 64 bytes, And the pointer is pointed to the end again.

Remarks block header and remarks text

Byte offset description
00-03 block Signature1 *(Indicates the data type in the block)
0-image (image field type)
1-text (remarks field type)
04-07 remark Length1 *(In bytes)
08-N remark text (n = length)
1 *When an integer is stored, the high byte comes first.

 

Seealso:

Http://msdn.microsoft.com/en-us/library/8599s21w%28v=VS.71%29.aspx

 

 
Format:
Supports null values.
Date-time, currency, and double-precision data
The character field and remarks field are marked as binary
Add a table to the database (. DBC) File
The following formula is used to obtain the number of fields in the table file: (x-296/32) in the formula, X indicates the position of the first record (8th to 9th bytes of the header record ), 296 indicates 263 (post-link information) + 1 (header record Terminator) + 32 (first field subrecord), and 32 indicates the length of the field subrecord.

Because the records of DBF Files are stored in the file data part in ASCII code, you only need to read the content of the file header and field type description area, you can directly read each record in the DBF file.

 

Refer:

Http://msdn.microsoft.com/en-us/library/st4a0s68%28v=VS.71%29.aspx

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.