Merge pull request #3378 from jlp765/streamsEx

Streams examples & default newFileStream() file mode
This commit is contained in:
Dominik Picheta
2015-09-27 13:46:04 +01:00
2 changed files with 34 additions and 1 deletions

View File

@@ -11,6 +11,26 @@
## the `FileStream` and the `StringStream` which implement the stream
## interface for Nim file objects (`File`) and strings. Other modules
## may provide other implementations for this standard stream interface.
##
## Examples:
##
## .. code-block:: Nim
##
## import streams
## var
## ss = newStringStream("""The first line
## the second line
## the third line""")
## line = ""
## while ss.readLine(line):
## echo line
## ss.close()
##
## var fs = newFileStream("somefile.txt", fmRead)
## if not isNil(fs):
## while fs.readLine(line):
## echo line
## fs.close()
include "system/inclrtl"
@@ -371,7 +391,7 @@ when not defined(js):
result.writeDataImpl = fsWriteData
result.flushImpl = fsFlush
proc newFileStream*(filename: string, mode: FileMode): FileStream =
proc newFileStream*(filename: string, mode: FileMode = fmRead): FileStream =
## 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.

View File

@@ -0,0 +1,13 @@
discard """
file: "tstreams2.nim"
output: '''fs is: nil'''
"""
import streams
var
fs = newFileStream("amissingfile.txt")
line = ""
echo "fs is: ",repr(fs)
if not isNil(fs):
while fs.readLine(line):
echo line
fs.close()