I have a binary file full with 32-bit unsigned longs in big-endian order, and I am trying to read some without loading the file in memory:
func GetVal ( f *os.File, gi int ) int {
pos := gi*4
var value uint8
newpos,seek_err := f.Seek(int64(pos), os.SEEK_SET)
if newpos != int64(pos) {
fmt.Fprintf(os.Stderr, "Can't seek to pos %d\n", pos)
os.Exit(1)
}
if seek_err != nil {
fmt.Fprintf(os.Stderr, "Can't seek to pos %d\n", pos)
os.Exit(1)
}
read_err := binary.Read(f, binary.BigEndian, &value)
if read_err != nil {
fmt.Fprintf(os.Stderr, "Can't read from file\n")
os.Exit(1)
}
return int(value)
}
func OpenDict(fname string) FileMap {
fh,err := os.Open(fname)
if err != nil {
fmt.Fprintf(os.Stderr, "File %s not found\n",fname)
os.Exit(1)
}
return fh
}
func main () {
dict := OpenDict("/path/to/binary/file")
pos := 213415
val := GetVal(dict, gi)
fmt.Printf("%d\n", val)
}
I am getting "0"s all the time (regardless the pos and the asssociated value in the file).
What am I doing wrong?
Thanks in advance,
M;
Shouldn't this be uint32?
El 09/05/2011, a las 18:16, Russ Cox escribió:
>> var value uint8
>
> Shouldn't this be uint32?
>
Ooops... I have been copy-pasting from a previous version of the function and didn't realized this.
Sorry for not figuring out before!
M;