func main() {
file, err := os.Open("example.dbf") // For read access.
if err != nil {
log.Fatal(err)
}
dBaseioReader, err := NewReader(file)
if err != nil {
log.Fatal(err)
}
return nil
}
type dbHeader struct {
Version byte
LastUpdate [3]byte
NumRecords int32
NumBytesInHeader int16
NumBytesInRecord int16
_ [2]byte //reserved
IncompatFlag byte
EncryptionFlag byte
MultiUserProcessing [12]byte
MDXProductionFlag byte
LangDriverId byte
_ [2]byte //reserved
LangDriverName [32]byte
_ [4]byte //reserved
}
type dbFieldDescriptor struct {
FieldName [32]byte
FieldType byte
FieldLen byte
FieldDec byte
_ [2]byte
MDXProductionFlag byte
_ [2]byte
NextAutoIncrement [4]byte
_ [4]byte
}
type DBaseReader struct {
rawInput *bufio.Reader
Header *dbHeader
Fields []*dbFieldDescriptor
recordsLeft int
}
func NewReader(input io.Reader) (dbr *DBaseReader, err error) {
dbr = &DBaseReader{
rawInput: bufio.NewReaderSize(input, 32*1024),
Header: &dbHeader{},
}
err = binary.Read(dbr.rawInput, binary.LittleEndian, dbr.Header)
if err != nil{
return
}
dbr.recordsLeft = int(dbr.Header.NumRecords)
headerBytesLeft := dbr.Header.NumBytesInHeader
headerBytesLeft -= dbHeaderSize
// read field descriptors until 0x0D termination byte
var term []byte
for {
field := &dbFieldDescriptor{}
err = binary.Read(dbr.rawInput, binary.LittleEndian, field)
if err != nil{
//FIRST CRASH HAPPENS HERE.
return
}
以上是相关代码。无论我使用什么示例 dbf 文件,程序都会崩溃。我不确定为什么我不断收到“意外的 EOF”错误。在过去的几天里,我一直试图解决这个问题,但不幸的是没有运气。
小唯快跑啊
相关分类