为什么删除声明但未使用的变量导致程序提前终止?

时间:2018-04-12 07:44:28

标签: nim

我无法想出这个。当我从以下proc中删除let size = s.readInt64()时,.exe似乎在它到达结束之前终止。声明但未使用!它必须去!

proc load(fn: string): Alternating =
  var s = newFileStream(fn, fmRead)
  let size = s.readInt64() #WITHOUT THIS LINE THE .exe doesn't execute ExitTerminal()
  result = newSeq[(float, int)]()
  while not s.atEnd:
    let element = (s.readFloat64.float, s.readInt64.int)
    result.add(element)
  s.close()

以下是整个计划。文件类型是.dat,我想它是二进制的。它是在程序中创建的。我编译了-d:release Nim版本0.18.0和minGW编译器

import streams

type
  Alternating = seq[(float, int)]

proc store(fn: string, data: Alternating) =
  var s = newFileStream(fn, fmWrite)
  s.write(data.len)
  for x in data:
    s.write(x[0])
    s.write(x[1])
  s.close()

proc load(fn: string): Alternating =
  var s = newFileStream(fn, fmRead)
  let size = s.readInt64() #WITHOUT THIS LINE THE .exe doesn't execute ExitTerminal()
  result = newSeq[(float, int)]()
  while not s.atEnd:
    let element = (s.readFloat64.float, s.readInt64.int)
    result.add(element)
  s.close()


let data = @[(1.0, 1), (2.0, 2)]

store("tmp.dat", data)
let dataLoaded = load("tmp.dat")

echo dataLoaded


### EXIT PROCEDURE FROM TERMINAL PROGRAM

proc ExitTerminal: bool =
  echo "Exit Application"
  echo "(y/n)"
  while true:
    case readline(stdin)
    of "y", "Y", "yes", "Yes": return true
    of "n", "N", "no", "No": return false
    else: echo "Please be clear: yes or no"


if ExitTerminal() == false: discard ExitTerminal()

1 个答案:

答案 0 :(得分:3)

很难分辨,因为我们对您正在阅读的文件格式一无所知。但通常你不能删除s.readInt64()只是因为它没有被使用,因为除了读取这个proc之外,你的流光标也会提前。如果您需要忽略该值,请使用discard s.readInt64() # Ignore size