[go-nuts] How does bufio.ReadBytes treat ctrl-c in windows?

240 views
Skip to first unread message

pallas0328

unread,
May 2, 2010, 11:15:07 PM5/2/10
to golang-nuts
My code is as follows
package main
import (
"fmt"
"bufio"
"os"
)
func main() {
rd:=bufio.NewReader(os.Stdin)
var line []byte
var err os.Error
for {
line,err=rd.ReadBytes(10)
fmt.Printf("\nline=%v\n",line)
fmt.Printf("err=%v\n",err)
if err!=nil {
break
}
}
}
When I input:
a [carrige return] b [ctrl-c]
I got:
line=[97 13 10]
err=<nil>
line=[]
err=EOF
The last line with 'b' just disappeared.
The pkg doc says:
ReadBytes reads until the first occurrence of delim in the input,
returning a string containing the data up to and including the
delimiter. If ReadBytes encounters an error before finding a
delimiter, it returns the data read before the error and the error
itself (often os.EOF). ReadBytes returns err != nil if and only if
line does not end in delim.
Is it the same in linux and windows?

Russ Cox

unread,
May 2, 2010, 11:19:33 PM5/2/10
to pallas0328, golang-nuts
> Is it the same in linux and windows?

Apparently not. I think that Windows does not
deliver the partial line when you type ^c, so there's
little Go can do about it.

Russ

brainman

unread,
May 3, 2010, 1:31:59 AM5/3/10
to golang-nuts
This program just dies on my windows or linux:

C:\Documents and Settings\brainman>\\sos\pub\test
a

line=[97 13 10]
err=<nil>
b

line=[98 13 10]
err=<nil>
ccccc^C
C:\Documents and Settings\brainman>

or

sos t2 # ./test
a

line=[97 10]
err=<nil>
b

line=[98 10]
err=<nil>
cccc^Csos t2 #


Maybe it is a coin toss if it gets to output to console before it
goes.

I think ReadBytes talks here about something completely different (not
Ctrl+C), but if EOF arrives before DELIMETER:

C:\TMP>type alex.txt
aaaa
bbb
ccc
C:\TMP>type alex.txt | \\sos\pub\test

line=[97 97 97 97 13 10]
err=<nil>

line=[98 98 98 13 10]
err=<nil>

line=[99 99 99]
err=EOF

C:\TMP>

or

sos t2 # cat a
aaaa
bbbb
ccccsos t2 # cat a | ./test

line=[97 97 97 97 10]
err=<nil>

line=[98 98 98 98 10]
err=<nil>

line=[99 99 99 99]
err=EOF
sos t2 #


Alex

befelemepeseveze

unread,
May 3, 2010, 3:28:02 AM5/3/10
to golang-nuts
On 3 kvě, 07:31, brainman <alex.brain...@gmail.com> wrote:
> Maybe it is a coin toss if it gets to output to console before it
> goes.

IIRC:

Typing Ctrl-C in console kills the console app (i.e. sending a SIGKILL
or so on *nix, don't know how on Windows).

Indicating an EOF while reading from keyboard: type Ctrl-D on *nix,
Ctrl-Z on Windows.
Reply all
Reply to author
Forward
0 new messages