dqst96444
2018-08-24 18:56
浏览 175
已采纳

在Golang中读取二进制.fbx文件

Unlike in other languages, I really couldn't figure out how to read a binary file into an array of bytes or just turn it into an ASCII-string, which puts me into quite a problem.

The code I've been using:

func TestFBX(fileName string) {
    file, err := os.Open(fileName)
    if (err != nil) {
        log.Fatal(err)
    }
    defer file.Close()

    var content []byte
    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        text := []byte(scanner.Text())
        buf := bytes.NewReader(text)
        err := binary.Read(buf, binary.LittleEndian, &content)

        if (err != nil) {
            fmt.Println(err)
        }
    }

    fmt.Printf("%v", content)
    fmt.Println("")

    if err := scanner.Err(); err != nil {
        log.Fatal(err)
    }
}

What it does in the end is print out [ ], an empty slice. Now when I tried putting float64 or int32 instead of []byte it did print out different numbers but I still honestly don't get how to read the whole file and not just whatever number is there at the beginning.

2条回答 默认 最新

相关推荐 更多相似问题