我做了一个简单的程序,它读取字符直到按下回车键
var data: string
while true:
var c = readChar(stdin) # read char
case c
of '\r': # if enter, stop
break
else: discard
data.add(c) # add the read character to the string
echo data
但是当它尝试时echo data
,它崩溃了
> ./program
hello
Traceback (most recent call last)
program.nim(11) program
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
这意味着data
为零。但是每次我按输入一个字符时,它都会将该字符添加到data
. 出了点问题,但在哪里?