summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--changelog.md2
-rw-r--r--lib/system/io.nim12
2 files changed, 13 insertions, 1 deletions
diff --git a/changelog.md b/changelog.md
index 32c6935db..451883dbb 100644
--- a/changelog.md
+++ b/changelog.md
@@ -18,7 +18,7 @@
 ## Library additions
 
 - `macros.newLit` now works for ref object types.
-
+- `system.writeFile` has been overloaded to also support `openarray[byte]`.
 
 ## Library changes
 
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