diff options
author | twetzel59 <twetzel59@gmail.com> | 2018-03-04 11:30:28 -0500 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2018-03-04 17:30:28 +0100 |
commit | 9079517d71a010e38a700870abf0703165e08b8b (patch) | |
tree | 836c2aa88063082ecb8b73bece829b8d6d87b597 /lib | |
parent | 4164ec4f8b6d9968e69381edd35d9cf6fe79dee1 (diff) | |
download | Nim-9079517d71a010e38a700870abf0703165e08b8b.tar.gz |
Resolves #5588: adds openFileStream proc that throws on failure (#7282)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/streams.nim | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/lib/pure/streams.nim b/lib/pure/streams.nim index 354e07da3..025a534c5 100644 --- a/lib/pure/streams.nim +++ b/lib/pure/streams.nim @@ -447,9 +447,19 @@ when not defined(js): ## creates a new stream from the file named `filename` with the mode `mode`. ## If the file cannot be opened, nil is returned. See the `system ## <system.html>`_ module for a list of available FileMode enums. + ## **This function returns nil in case of failure. To prevent unexpected + ## behavior and ensure proper error handling, use openFileStream instead.** var f: File if open(f, filename, mode, bufSize): result = newFileStream(f) + proc openFileStream*(filename: string, mode: FileMode = fmRead, bufSize: int = -1): FileStream = + ## creates a new stream from the file named `filename` with the mode `mode`. + ## If the file cannot be opened, an IO exception is raised. + var f: File + if open(f, filename, mode, bufSize): + return newFileStream(f) + else: + raise newEIO("cannot open file") when true: discard |