Reputation: 3867
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!
Upvotes: 0
Views: 575
Reputation: 48330
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
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.
Upvotes: 1