3

I liked to read the whole content of a text file using Nim.

I tried

let fileContent: string = readAll("file.txt")

but this doesn't compile.

Quonux
  • 2,975
  • 1
  • 24
  • 32

2 Answers2

7

The readAll proc requires as parameter a File, which is what open returns. However, for a one liner you could use readFile:

let fileContent = readFile("file.txt")
Grzegorz Adam Hankiewicz
  • 7,349
  • 1
  • 36
  • 78
2

It is most easily done so:

let filepath: string = "file.txt"
let f = open(filepath, fmRead)
let fileContent: string = readAll(f)
f.close()

(nothing has to get imported to do that)

Dimitri Lesnoff
  • 317
  • 1
  • 14
Quonux
  • 2,975
  • 1
  • 24
  • 32