Files, navigation and fields
Opening and creating files, moving between records, and reading and writing fields.
Opening and Creating Files#
open(pPath) → true#
Opens an existing .dbf file. If the file cannot be opened for read/write it is opened read-only. The companion .fpt memo file is opened automatically when present.
After a successful open, the current record is positioned at record 1 (goTop() is called internally). If the file has no records, isEof() and isBof() are both true.
Raises if the file cannot be opened at all.
oDbf = new DBFFile
oDbf.open("customers.dbf")create(pPath, pFieldDefs) → true#
Creates a new .dbf file with the given field definitions. If any field has type M or G, a companion .fpt file is created automatically.
The file version is set to C_DBF_VERSION_DBASE3 (no memo) or C_DBF_VERSION_FOXPRO (with memo). The code page defaults to C_CODEPAGE_WIN_1252.
Raises if the field list is empty or the file cannot be created.
oDbf = new DBFFile
oDbf.create("orders.dbf", [
["ORDERID", "N", 8, 0],
["CUSTNAME", "C", 40, 0],
["AMOUNT", "N", 12, 2],
["ORDERDATE","D", 8, 0],
["NOTES", "M", 10, 0]
])close()#
Flushes any pending record changes to disk, writes the final file header, and closes both the .dbf and .fpt file handles. Always call close() when finished; not calling it may leave the last modified record unsaved.
oDbf.close()Navigation#
The library maintains a current record pointer. Navigation methods update this pointer and load the record into an internal buffer. Two boolean flags track boundary state:
- BOF (Beginning Of File) —
truewhen the pointer is at or before record 1. - EOF (End Of File) —
truewhen the pointer is past the last record.
When both are true the table is empty.
goTop()#
Moves to the first record and reads it. Sets isBof() = true, isEof() = false. If the table is empty, sets both to true.
goBottom()#
Moves to the last record and reads it. Sets isBof() = false, isEof() = false. If the table is empty, sets both to true.
goTo(pRec)#
Moves to the record at physical position pRec (1-based). If pRec is less than 1, sets isBof() = true. If pRec is greater than recCount(), sets isEof() = true.
skip(pCount)#
Moves forward or backward by pCount records. Positive values move forward; negative values move backward. Sets EOF or BOF if the pointer goes past the boundaries.
oDbf.goTop()
while ! oDbf.isEof()
# process record
oDbf.skip(1)
endisEof() → true / false#
Returns true when the current position is past the last record.
isBof() → true / false#
Returns true when the current position is at or before record 1, or when the table is empty.
recNo() → number#
Returns the 1-based physical position of the current record. Returns 0 when at BOF and recCount() + 1 when at EOF.
recCount() → number#
Returns the total number of records in the table, including soft-deleted ones.
isDeleted() → true / false#
Returns true if the current record has been soft-deleted with deleteRec().
Reading Fields#
fieldGet(pFieldName) → value#
Returns the value of the named field in the current record. Works uniformly across all field types including memo fields.
| Field Type | Return Value |
|---|---|
C | String (space-padded to field width) |
N, F | Trimmed numeric string (e.g. "85000.50") |
D | Trimmed date string in YYYYMMDD format |
L | true or false |
I | Number (decoded from 4-byte little-endian binary) |
M, G | Memo text string read from the FPT file |
Use trim() on C, N, F, and D values to strip padding.
Raises if the field name does not exist.
cName = trim(oDbf.fieldGet("NAME"))
nSalary = number(oDbf.fieldGet("SALARY"))
lActive = oDbf.fieldGet("ACTIVE")
cDate = oDbf.fieldGet("HIREDATE") # e.g. "20230115"
cNotes = oDbf.fieldGet("NOTES") # memo — works like any other fieldfieldName(pIdx) → string#
Returns the name of field number pIdx (1-based). Returns "" if out of range.
fieldType(pIdx) → string#
Returns the single-character type code of field number pIdx.
fieldLen(pIdx) → number#
Returns the storage length in bytes of field number pIdx.
fieldDec(pIdx) → number#
Returns the number of decimal places of field number pIdx.
fieldCount() → number#
Returns the total number of fields in the table.
for i = 1 to oDbf.fieldCount()
? oDbf.fieldName(i) + " " + oDbf.fieldType(i) + " " + oDbf.fieldLen(i)
nextWriting Fields#
fieldPut(pFieldName, pValue)#
Sets the value of the named field in the current record. Works uniformly across all field types including memo fields. The record is not written to disk until the next navigation call or close().
Raises if the field name does not exist, the file is read-only, or a numeric value is too wide for its field.
oDbf.fieldPut("NAME", "Sara Abdullah")
oDbf.fieldPut("SALARY", 91000.00)
oDbf.fieldPut("ACTIVE", true)
oDbf.fieldPut("NOTES", "Project manager, joined 2021.") # memoreplace(pFieldName, pValue)#
Alias for fieldPut(). Provided for familiarity with FoxPro/dBASE syntax. Identical behaviour in every respect.
oDbf.replace("NAME", "Sara Abdullah")
oDbf.replace("SALARY", 91000.00)
oDbf.replace("NOTES", "Updated bio.") # memo — no special handling needed