I am using bufio.Reader and binary.Read to read binary from file, I want to seek in the file or bufio.Reader Below code does not work
fi.Seek(0, os.SEEK_SET) ////Does not work for bufio.Reader below definition shows that r control the position , but I did not see any method to set it type Reader struct { buf []byte rd io.Reader // reader provided by the client r, w int // buf read and write positions err error lastByte int lastRuneSize int } I expect below code to output 3.13 twice , but it output 3.13 3.14 package main import ( _ "bytes" "fmt" _ "math" "bufio" "encoding/binary" "os" ) func main() { var pi float64 //b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40, // 0x78, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40} //buf := bytes.NewReader(b) //err := binary.Read(buf, binary.LittleEndian, &pi) //if err != nil { // fmt.Println("binary.Read failed:", err) //} //fmt.Println(pi) //// Output: 3.141592653589793 //err = binary.Read(buf, binary.LittleEndian, &pi) //if err != nil { // fmt.Println("binary.Read failed:", err) //} //fmt.Println(pi) //------------------------------------------------------------ // open output file fo, err := os.Create("./output.bin") if err != nil { panic(err) } // make a write buffer var fl1 float64 = 3.13 var fl2 float64 = 3.14 w := bufio.NewWriter(fo) err = binary.Write(w, binary.LittleEndian, fl1) if err != nil { fmt.Println("binary.Write failed:", err) } err = binary.Write(w, binary.LittleEndian, fl2) if err != nil { fmt.Println("binary.Write failed:", err) } w.Flush() // close fo on exit and check for its returned error if err := fo.Close(); err != nil { panic(err) } //------------------------------------------------------------ fi, err := os.Open("./output.bin") if err != nil { panic(err) } // close fi on exit and check for its returned error defer func() { if err := fi.Close(); err != nil { panic(err) } }() rd := bufio.NewReader(fi) err = binary.Read(rd, binary.LittleEndian, &pi) if err != nil { fmt.Println("binary.Read failed:", err) } fmt.Println(pi) fi.Seek(0, os.SEEK_SET) ////Does not work for bufio.Reader err = binary.Read(rd, binary.LittleEndian, &pi) if err != nil { fmt.Println("binary.Read failed:", err) } fmt.Println(pi) } -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.