普通网友 2018-03-04 13:22
浏览 196
已采纳

为什么f1.Read(buf)不将内容读出到buf?

package main

import (
    "fmt"
    "os"
    "io"
)

func main() {

    f1,_ := os.Create("f1")

    io.WriteString(f1, "some content")

    buf := make([]byte, 8)

    f1.Read(buf)

    fmt.Println(buf)

}

I create a file, then write in some string. then read out, but there is no content.

The output is :

go run test.go
[0 0 0 0 0 0 0 0]
  • 写回答

3条回答 默认 最新

  • dte49889 2018-03-04 14:59
    关注

    In Go, don't ignore errors. When writing to and reading from a file, keep track of the current file offset.

    After the write the offset is at the end-of-file, you need to set the offset to the start-of-file before the read. For example, with diagnostic information,

    package main
    
    import (
        "fmt"
        "io"
        "os"
    )
    
    func main() {
        f1, err := os.Create("f1")
        if err != nil {
            fmt.Println(err)
            return
        }
        defer f1.Close()
        // The file offset is its current location.
        s, err := f1.Seek(0, io.SeekCurrent)
        if err != nil {
            fmt.Println(s, err)
            return
        }
        fmt.Println("offset:", s)
        // writing takes place at the file offset, and
        // the file offset is incremented by the number of bytes actually
        // written.
        n, err := f1.WriteString("some content")
        fmt.Println("write: ", n, err)
        if err != nil {
            fmt.Println(n, err)
            return
        }
        // The file offset is its current location
        s, err = f1.Seek(0, io.SeekCurrent)
        if err != nil {
            fmt.Println(s, err)
            return
        }
        fmt.Println("offset:", s)
    
        buf := make([]byte, 8)
    
        // the read operation commences at the
        // file offset, and the file offset is incremented by the number of
        // bytes read.  If the file offset is at or past the end of file, no
        // bytes are read, and read() returns zero.
        n, err = f1.Read(buf[:cap(buf)])
        fmt.Println("read:  ", n, err)
        buf = buf[:n]
        fmt.Println("buffer:", len(buf), buf)
        if err != nil {
            if err != io.EOF {
                fmt.Println(n, err)
                return
            }
        }
    
        // The file offset is set to the start-of-file.
        s, err = f1.Seek(0, io.SeekStart)
        if err != nil {
            fmt.Println(s, err)
            return
        }
        fmt.Println("offset:", s)
        // the read operation commences at the
        // file offset, and the file offset is incremented by the number of
        // bytes read.  If the file offset is at or past the end of file, no
        // bytes are read, and read() returns zero.
        n, err = f1.Read(buf[:cap(buf)])
        fmt.Println("read:  ", n, err)
        buf = buf[:n]
        fmt.Println("buffer:", len(buf), buf)
        if err != nil {
            if err != io.EOF {
                fmt.Println(n, err)
                return
            }
        }
    }
    

    Playground: https://play.golang.org/p/hPUn1ltKP2t

    Output:

    offset: 0
    write:  12 <nil>
    offset: 12
    read:   0 EOF
    buffer: 0 []
    offset: 0
    read:   8 <nil>
    buffer: 8 [115 111 109 101 32 99 111 110]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记