system.writeFile has been overloaded to also support openarray[byte] (#12313)

This commit is contained in:
zah
2019-10-02 21:34:03 +03:00
committed by Andreas Rumpf
parent 2446570350
commit ad13e18c7c
2 changed files with 13 additions and 1 deletions

View File

@@ -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

View File

@@ -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