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?

图片转代码服务由CSDN问答提供 功能建议

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

我想从stdin流中按字节读取。 与 Read 方法不同, ReadByte 似乎不返回 io.EOF 如果所有字节都已读取,如何中断?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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 Enter, 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 Enter, 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.

    打赏 评论

相关推荐 更多相似问题