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'm currently Director of Product (Terraform) @ HashiCorp, and we're hiring! If you'd like to come and work with me and help make Terraform Cloud even more amazing we have multiple positions opening in Product ManagementDesign, and Engineering & Engineering Management across a range of levels (i.e., junior through to senior). Please send in an application ASAP so we can get in touch.