duanpie4763 2017-08-23 08:29
浏览 58
已采纳

如何从标准输入中按字节读取?

in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
for {
    c, err := in.ReadByte()
    if err == io.EOF {
        break
    }
    out.WriteByte(c)
}

I want to read bytewise from the stdin stream. Unlike the Read methodReadByte doesn't seem to return io.EOF. How can I break if all bytes have been read?

  • 写回答

1条回答 默认 最新

  • dongyuan1160 2017-08-23 08:39
    关注

    This is not an issue of the Reader.ReadByte() implementation, nor that of bufio.NewReader().

    See this example to prove it:

    buf := bytes.NewBufferString("Hello World!
    ")
    in := bufio.NewReader(buf)
    for {
        c, err := in.ReadByte()
        if err == io.EOF {
            break
        }
        fmt.Print(string(c))
    }
    

    When running, the above prints

    Hello World!
    

    And terminates properly.

    Your issue is with os.Stdin. Reading from it is specific to its source. If it is your terminal, reading from it simply blocks and does not report io.EOF. See this example to prove it:

    in := bufio.NewReader(os.Stdin)
    for {
        fmt.Println("Reading.")
        c, err := in.ReadByte()
        if err == io.EOF {
            break
        }
        fmt.Print(string(c))
    }
    

    Its output is:

    Reading.
    

    And nothing happens. There is no new iteration, it is blocked. Now if you enter a line and press <kbd>Enter</kbd>, e.g. you enter Go!, output will be:

    Go!
    GReading.
    oReading.
    !Reading.
    
    Reading.
    

    And again, waits for new input. As you can see, data is fed / available per line. This is what your terminal does: while you enter your line, it is not sent to os.Stdin. Once you press <kbd>Enter</kbd>, the whole line is fed and is available from os.Stdin. This is what we see: each letter of the input Go! and a newline character. And we see the Reading. text printed for each iteration. After the input is consumed, in.ReadByte() is blocked again, waiting for new input. It does not report io.EOF.

    Now try the following: create a file e.g. a.txt and edit it to have one line: Go! and a newline. Now feed this file as the standard input to your program:

    go run play.go < a.txt
    

    Running it we'll see:

    Reading.
    GReading.
    oReading.
    !Reading.
    
    Reading.
    
    Reading.
    

    And it terminates so it works! It works because this time the source of os.Stdin is not your console / terminal, but the contents of a file, and once it's consumed, attempting to read from os.Stdin will properly report io.EOF.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行