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.

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

报告相同问题?

悬赏问题

  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿