Nim: How to write a file

Nim: How to write a file

Writing a file is as straight-forward as reading one:

let text = "Hello World!"
writeFile("hello.txt", text)

Writing each line incrementally is also similar:

proc writeHello() =
  let lines = ["Hello", "World", "!!"]
  let f = open("hello.txt", fmWrite) # fmWrite is the file mode constant
  defer: f.close()

  for line in lines:
    f.writeLine(line)

writeHello()

Published: 22/03/2022

Hi, I'm Glenn! 👋

I've spent most of my career working with or at startups. I'm currently the VP of Product / GTM @ 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 1.0, Terraform Cloud, and a whole host of amazing capabilities that 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.