普通网友 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条)

报告相同问题?

悬赏问题

  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。