普通网友 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 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图