Nim: How to read a file | Glenn Gillen
Nim: How to read a file

Nim: How to read a file

Mar 22, 2022

Reading the entire file in as a string is a simple one-shot:

let contents = readFile("hello.txt")
echo contents # prints the entire file

If you want to read it incrementally then:

proc readMyFile() =
let f = open("hello.txt")
# Close the file object when you are done
defer: f.close()
let line = f.readLine()
echo line
readMyFile() # will only read the first line
Hi, I'm Glenn! 👋 I've spent most of my career working with or at startups. I'm currently the Director of Product @ Ockam where I'm helping developers build applications and systems that are secure-by-design. It's time we started securely connecting apps, not networks.

Previously I led the Terraform product team @ HashiCorp, where we launched Terraform Cloud and set the stage for a successful IPO. Prior to that I was part of the Startup Team @ AWS, and earlier still an early employee @ Heroku. I've also invested in a couple of dozen early stage startups.