EOF when read binary file

Hello, I am working with a binary file. What I’m trying to do is write a slice to the file, and then read it to check that everything is fine. However, when reading the file, I get an error
which says ‘Unexpected EOF’. The offset within the disk is correct, the end of the file has not been reached, however the error occurs. What can be?

func initializeBMInode(ptrDisk *os.File, numStructs, iniBitmap int64) {
	total := 5 * numStructs
	for i := int64(0); i < total; i++ {
		ctr := '0'
		ptrDisk.Seek(iniBitmap+i, 0)
		var buffer bytes.Buffer
		binary.Write(&buffer, binary.BigEndian, &ctr)
		writeNextBytes(ptrDisk, buffer.Bytes())
	}
	/*
		Test bitmap
	*/
 	bmGen := make([]byte, 5*numStructs)
	ptrDisk.Seek(iniBitmap, 0)
	fmt.Println("InitBitmap")
	fmt.Println(iniBitmap)
	content := readNextBytes(ptrDisk, int64(unsafe.Sizeof(bmGen)))
 	buffer := bytes.NewBuffer(content)
	err := binary.Read(buffer, binary.BigEndian, &bmGen)
	if err != nil {
		log.Fatal(err) // Get error
	}
	fmt.Println(bmGen)
}
1 Like

unsafe.Sizeof(bmGen) is the size of the slice header.

package main

import (
	"fmt"
	"unsafe"
)

func main() {
	// runtime - slice header
	type slice struct {
		array unsafe.Pointer
		len   int
		cap   int
	}
	fmt.Println(int64(unsafe.Sizeof(slice{})))

	// bmGen - slice header
	var numStructs int64 = 1
	bmGen := make([]byte, 5*numStructs)
	fmt.Println(int64(unsafe.Sizeof(bmGen)))

	// bmGen - slice element
	fmt.Println(int64(unsafe.Sizeof(bmGen[0])))
}

https://play.golang.org/p/dSkDhR_wKP-

24
24
1
1 Like

Thank you very much, now I understand. Regards.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.