亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無論我使用什么 dBase (.dbf) 文件,binary.read 都會返回“意外的 EOF”

無論我使用什么 dBase (.dbf) 文件,binary.read 都會返回“意外的 EOF”

Go
慕森王 2021-11-15 15:48:46
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”錯誤。在過去的幾天里,我一直試圖解決這個問題,但不幸的是沒有運氣。
查看完整描述

1 回答

?
小唯快跑啊

TA貢獻1863條經驗 獲得超2個贊

您沒有提供任何證據證明您的文件格式是正確的。在編寫程序之前,您應該確認文件格式正確。


文件的前 256 個字節是多少?例如,


hex.go:


package main


import (

    "encoding/hex"

    "fmt"

    "io/ioutil"

    "os"

    "strconv"

)


func main() {

    if len(os.Args) <= 1 {

        fmt.Fprintln(os.Stderr, "usage: hex filename [bytes]")

        return

    }

    data, err := ioutil.ReadFile(os.Args[1])

    if err != nil {

        fmt.Fprintln(os.Stderr, "filename:", err)

        return

    }

    n := len(data)

    if len(os.Args) > 2 {

        i, err := strconv.Atoi(os.Args[2])

        if err != nil {

            fmt.Fprintln(os.Stderr, "bytes:", err)

            return

        }

        if n > i {

            n = i

        }

    }

    fmt.Print(hex.Dump(data[:n]))

}

輸出:


$ go run hex.go example.dbf 256

00000000  03 01 04 18 01 00 00 00  41 07 d0 05 00 00 00 00  |........A.......|

00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 03 00 00  |................|

00000020  54 52 41 43 4b 5f 49 44  00 00 00 43 01 00 00 00  |TRACK_ID...C....|

00000030  0b 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

00000040  4c 4d 55 4c 54 00 00 00  00 00 00 4c 0c 00 00 00  |LMULT......L....|

00000050  01 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

00000060  4e 54 41 58 59 45 41 52  00 00 00 4e 0d 00 00 00  |NTAXYEAR...N....|

00000070  04 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

00000080  4e 43 4f 55 4e 54 59 43  4f 44 00 4e 11 00 00 00  |NCOUNTYCOD.N....|

00000090  02 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

000000a0  43 50 52 4f 50 41 44 44  00 00 00 43 13 00 00 00  |CPROPADD...C....|

000000b0  3c 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |<...............|

000000c0  4c 43 4f 4d 4d 49 4e 44  00 00 00 4c 4f 00 00 00  |LCOMMIND...LO...|

000000d0  01 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

000000e0  4c 56 41 43 4c 41 4e 44  00 00 00 4c 50 00 00 00  |LVACLAND...LP...|

000000f0  01 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

$

表級


但是一張桌子的水平是多少?級別表示其版本。dBASE 表格式是一種隨著時間的推移而發展的標準。當 dBASE 的新版本對該格式進行一些改進時,會給出一個新的格式級別編號,與新的 dBASE 版本相同。例如,我們有級別 3、4、5 和 7,分別對應于 dBASE III、dBASE IV、dBASE 5 和 Visual dBASE 7。沒有級別 6,因為沒有 Visual dBASE 6。


7 級帶來了許多改進。字段名稱最多可包含 31 個字符(之前最多為 10 個)。出現了一些新的字段類型(例如,AutoIncrement 字段幾乎不可能為同一個表中的兩個記錄提供相同的數字)。如果您的表必須被其他軟件使用,您可能必須為了兼容性而犧牲這些優勢,因為很少有應用程序可以使用 7 級表。


.dbf 文件格式:


文件頭字節 0,位 0-2 表示版本號:dBASE Level 5 為 3,dBASE Level 7 為 4。


查看完整回答
反對 回復 2021-11-15
  • 1 回答
  • 0 關注
  • 274 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號