Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nim: read all content of text file

Tags:

nim-lang

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.

like image 679
Quonux Avatar asked Jan 25 '26 14:01

Quonux


2 Answers

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")
like image 57
Grzegorz Adam Hankiewicz Avatar answered Jan 29 '26 04:01

Grzegorz Adam Hankiewicz


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)

like image 37
Quonux Avatar answered Jan 29 '26 06:01

Quonux



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!