Quonux
Quonux

Reputation: 2991

Nim: read all content of text file

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.

Upvotes: 3

Views: 875

Answers (2)

Grzegorz Adam Hankiewicz
Grzegorz Adam Hankiewicz

Reputation: 7661

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")

Upvotes: 7

Quonux
Quonux

Reputation: 2991

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)

Upvotes: 2

Related Questions