gpt4 book ai didi

Read bytes from file, save it to struct and modify values(从文件中读取字节,将其保存到结构并修改值)

转载 作者:bug小助手 更新时间:2023-10-25 14:50:10 27 4
gpt4 key购买 nike



I am a completely newbie to Go. I watched the Coursera Introduction to Go Language, but some topics are not covered in depth.

我完全是个新手。我看了《Coursera围棋语言导论》,但有些话题没有深入讨论。


To practice Go I'm solving as many exercises as I can from CS50 . I'm now working on an example in which I need to read a bmp file, copy the headers content to the correspondent data structure, change some parameters in this structure, and then copy the result to a new file.

为了练习围棋,我尽可能多地做CS50的练习。我现在正在处理一个示例,其中我需要读取一个BMP文件,将头内容复制到相应的数据结构,更改该结构中的一些参数,然后将结果复制到一个新文件。


The first thing I noticed is that I cannot read as binary the number of bytes correspondent to the size of the struct I created. This is due to padding that may happen when building a struct. I was able to read field by field and filled up the struct.

我注意到的第一件事是,我不能将与我创建的结构的大小相对应的字节数读作二进制。这是由于在构建结构时可能发生的填充。我能够逐个字段地读取,并填充了结构。


In order to improve this, I tried to iterate through the structure, get the size of each field, read from the file the correspondent number of bytes and then write to the file, again, after iterating through the structure.

为了改进这一点,我试图迭代结构,获取每个字段的大小,从文件中读取相应的字节数,然后在迭代结构之后再次写入文件。


I'm stuck in the last step (if the previous steps were done correctly): How can I correctly set the values depending on the type of field?

我陷入了最后一步(如果前面的步骤正确完成的话):我如何才能根据字段类型正确设置值?



package main

import (
"bufio"
"encoding/binary"
"fmt"
"os"
"reflect"
)

type DWORD uint32
type WORD uint16
type BITMAPFILEHEADER struct {
BfType WORD
BfSize DWORD
BfReserved1 WORD
BfReserved2 WORD
BfOffBits DWORD
}

func main() {
input_file, err := os.Open("harvard_bottomup.bmp")
if err != nil {
fmt.Println("Could not open the origin file.")
fmt.Println(err)
}

var bf BITMAPFILEHEADER
reader := bufio.NewReader(input_file)

value := reflect.ValueOf(&bf).Elem()
n := value.NumField()
structType := value.Type()
for i := 0; i < n; i++ {

b := make([]byte, structType.Field(i).Type.Size())
err = binary.Read(reader, binary.BigEndian, b)
if err != nil {
fmt.Println(err)
}

fmt.Printf("Field Name: %-15s %-15s %02x\n", structType.Field(i).Name, structType.Field(i).Type, b)
// I should now set the value, but I don't know how to switch between types
// value.Field(i).Set...
}

input_file.Close()
}

PS: Before that I tried to read directly to the fields within the struct, but I could not do that.

PS:在此之前,我尝试直接读取结构中的字段,但我做不到。


更多回答
优秀答案推荐

It looks like you are duplicating functionality in the binary.Read function. The function decodes data in the specified byte order to successive fields in a struct. You can view struct decoding code here.

看起来像是在复制二进制文件中的功能。Read函数。该函数将数据按照指定的字节顺序解码为结构中的连续字段。您可以在此处查看结构解码代码。


Here's how to use Read to decode the file header:

以下是如何使用Read对文件头进行解码:


input_file, err := os.Open("harvard_bottomup.bmp")
if err != nil {
fmt.Println("Could not open the origin file.")
fmt.Println(err)
return
}
defer input_file.Close()
reader := bufio.NewReader(input_file)

var bf BITMAPFILEHEADER
err = binary.Read(reader, binary.BigEndian, &bf)
if err != nil {
fmt.Println(err)
return
}

更多回答

Hi @Brother Constant, thank you very much for your answer. This indeed worked, thanks for the help, I will take a look at the references you provided so I can have a better understanding of how Go works!

嗨@恒定兄弟,非常感谢你的回答。这确实奏效了,谢谢你的帮助,我会看看你提供的参考资料,这样我就可以更好地了解围棋是如何工作的!

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com