Writing multiple structures to a File in Go

huangapple go评论80阅读模式
英文:

Writing multiple structures to a File in Go

问题

我和我的团队刚开始学习Go语言,我们有一个"Header"结构体和多个"Record"结构体,我们想将它们写入文件。然而,每当我们尝试通过重写文件中的Header结构体来更新它时,文件的其余部分都会出现问题。

我们正在使用Encode / Decode:(dataFile是从os.Open返回的)

dataFile.Seek(header.FreePtr,0) //定位到空闲空间 - 我们可以重构并定位到文件末尾吗?
encoder := gob.NewEncoder((dataFile))
err = encoder.Encode(record)
if err != nil {
panic(err)
}

dataFile.Seek(header.FreePtr, 0)
decoder = gob.NewDecoder(dataFile)
r := Record{}
err = decoder.Decode(&r)
fmt.Println(r.Key)
fmt.Println(r.Width)
fmt.Println(string(r.Data))

header.FreePtr += int64(unsafe.Sizeof(record.Key)) + int64(unsafe.Sizeof(record.Width))+ int64(record.Width)
dataFile.Seek(0, 0)
encoder = gob.NewEncoder(dataFile)
err = encoder.Encode(header)
if err != nil {
panic(err)
}

有没有更好的方法来解决这个问题?如果我们不需要更新header,那么这样做是否能解决我们的问题?(每次都将编码到文件末尾,而不是尝试在添加记录之间更新开头的内容)。理想情况下,我们以后可能还需要header,所以如果我们能保留它,那就太好了。

提前感谢!

英文:

me and my group are new to Go and we have a "Header" struct and multiple "Record" structs that we're trying to write to a file. However, whenever we try to update the Header struct in the file by rewriting to it, the rest of the file gets messed up.

We are using Encode / Decode: (dataFile is returned from os.Open)

dataFile.Seek(header.FreePtr,0) //seek to free space - could we just refactor and seek to end of file? 
encoder := gob.NewEncoder((dataFile))
err = encoder.Encode(record)
if err != nil {
	panic(err)
}

dataFile.Seek(header.FreePtr, 0)
decoder = gob.NewDecoder(dataFile)
r := Record{}
err = decoder.Decode(&r)
fmt.Println(r.Key)
fmt.Println(r.Width)
fmt.Println(string(r.Data))

header.FreePtr += int64(unsafe.Sizeof(record.Key)) + int64(unsafe.Sizeof(record.Width))+ int64(record.Width)
dataFile.Seek(0, 0)
encoder = gob.NewEncoder(dataFile)
err = encoder.Encode(header)
if err != nil {
	panic(err)
}

Is there a better way to do this? If we don't need to update the header, would that solve our problems? (Encoding to the end of the file all the time instead of trying to update something at the beginning between adding records). Ideally we might need a header later though, so if we could keep it, that would be great.

Thanks in advance!

答案1

得分: 1

你的文件混乱了,因为当你更新头部时,头部的长度发生了变化。这就是为什么一些格式会将文件的最后N个字节保留给头部的原因。按照你的方式,你需要:

  1. 将整个文件读入内存。
  2. 更新头部。
  3. 将头部和记录重新写回文件。

或者,在文件开头为头部分配一个固定大小,并只更新该部分。

请记住,这是任何编程语言都会遇到的问题,不仅仅是Go语言。

英文:

Your file get messed up because the length of the header changes when you update it.
That is why some formats reserve the last N bytes of the File for the header.
In your way you would have to

  1. Read the whole file in memory
  2. Update the Header
  3. Rewrite Header + Records back to the file

Or Allocate a FIXED size for the header at the start of your file and only update that part of it.

Keep in mind that this a problem with any programming language, not just Go.

huangapple
  • 本文由 发表于 2014年10月21日 23:34:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/26490614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定