From ad13e18c7cd008da418770b7f1b8eae696992ced Mon Sep 17 00:00:00 2001 From: zah Date: Wed, 2 Oct 2019 21:34:03 +0300 Subject: `system.writeFile` has been overloaded to also support `openarray[byte]` (#12313) --- lib/system/io.nim | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'lib/system') diff --git a/lib/system/io.nim b/lib/system/io.nim index a39e8cf94..7ba36a30d 100644 --- a/lib/system/io.nim +++ b/lib/system/io.nim @@ -679,6 +679,18 @@ proc writeFile*(filename, content: string) {.tags: [WriteIOEffect], benign.} = else: sysFatal(IOError, "cannot open: " & filename) +proc writeFile*(filename: string, content: openArray[byte]) = + ## Opens a file named `filename` for writing. Then writes the + ## `content` completely to the file and closes the file afterwards. + ## Raises an IO exception in case of an error. + var f: File + if open(f, filename, fmWrite): + try: + f.writeBuffer(unsafeAddr content[0], content.len) + finally: + close(f) + else: + raise newException(IOError, "cannot open: " & filename) proc readLines*(filename: string, n = 1.Natural): seq[TaintedString] = ## read `n` lines from the file named `filename`. Raises an IO exception -- cgit 1.4.1-2-gfad0