2024-06-27 11:48:32 +00:00
|
|
|
{.deprecated: "use https://nim-lang.org/docs/syncio.html#writeFile%2Cstring%2CopenArray%5Bbyte%5D directly".}
|
|
|
|
|
2019-10-02 13:54:13 +03:00
|
|
|
when not compiles(writeFile("filename", @[byte(1)])):
|
2021-12-02 16:24:02 +01:00
|
|
|
proc writeFile*(filename: string, content: openArray[byte]) =
|
2019-10-02 13:54:13 +03:00
|
|
|
## 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)
|