Source Edit

This module implements various synchronized I/O operations.

Imports

since, formatfloat, widestrs, exitprocs

Types

  1. File = ptr CFile

The type representing a file handle. Source Edit

  1. FileHandle = cint

type that represents an OS file handle; this is useful for low-level file access Source Edit

  1. FileMode = enum
  2. fmRead, ## Open the file for read access only.
  3. ## If the file does not exist, it will not
  4. ## be created.
  5. fmWrite, ## Open the file for write access only.
  6. ## If the file does not exist, it will be
  7. ## created. Existing files will be cleared!
  8. fmReadWrite, ## Open the file for read and write access.
  9. ## If the file does not exist, it will be
  10. ## created. Existing files will be cleared!
  11. fmReadWriteExisting, ## Open the file for read and write access.
  12. ## If the file does not exist, it will not be
  13. ## created. The existing file will not be cleared.
  14. fmAppend ## Open the file for writing only; append data
  15. ## at the end. If the file does not exist, it
  16. ## will be created.

The file mode when opening a file. Source Edit

  1. FileSeekPos = enum
  2. fspSet, ## Seek to absolute value
  3. fspCur, ## Seek relative to current position
  4. fspEnd ## Seek relative to end

Position relative to which seek should happen. Source Edit

Vars

  1. stderr {.importc: "stderr", header: "<stdio.h>".}: File

The standard error stream. Source Edit

  1. stdin {.importc: "stdin", header: "<stdio.h>".}: File

The standard input stream. Source Edit

  1. stdout {.importc: "stdout", header: "<stdio.h>".}: File

The standard output stream. Source Edit

Procs

  1. proc close(f: File) {....tags: [], gcsafe, sideEffect, ...raises: [], forbids: [].}

Closes the file. Source Edit

  1. proc endOfFile(f: File): bool {....tags: [], gcsafe, raises: [], forbids: [].}

Returns true if f is at the end. Source Edit

  1. proc flushFile(f: File) {....tags: [WriteIOEffect], raises: [], forbids: [].}

Flushes f’s buffer. Source Edit

  1. proc getFileHandle(f: File): FileHandle {....raises: [], tags: [], forbids: [].}

Returns the file handle of the file f. This is only useful for platform specific programming. Note that on Windows this doesn’t return the Windows-specific handle, but the C library’s notion of a handle, whatever that means. Use getOsFileHandle instead. Source Edit

  1. proc getFilePos(f: File): int64 {....gcsafe, raises: [IOError], tags: [],
  2. forbids: [].}

Retrieves the current position of the file pointer that is used to read from the file f. The file’s first byte has the index zero. Source Edit

  1. proc getFileSize(f: File): int64 {....tags: [ReadIOEffect], gcsafe,
  2. raises: [IOError], forbids: [].}

Retrieves the file size (in bytes) of f. Source Edit

  1. proc getOsFileHandle(f: File): FileHandle {....raises: [], tags: [], forbids: [].}

Returns the OS file handle of the file f. This is only useful for platform specific programming. Source Edit

  1. proc open(f: var File; filehandle: FileHandle; mode: FileMode = fmRead): bool {.
  2. ...tags: [], raises: [], gcsafe, forbids: [].}

Creates a File from a filehandle with given mode.

Default mode is readonly. Returns true if the file could be opened.

The passed file handle will no longer be inheritable.

Source Edit

  1. proc open(f: var File; filename: string; mode: FileMode = fmRead;
  2. bufSize: int = -1): bool {....tags: [], raises: [], gcsafe, forbids: [].}

Opens a file named filename with given mode.

Default mode is readonly. Returns true if the file could be opened. This throws no exception if the file could not be opened.

The file handle associated with the resulting File is not inheritable.

Source Edit

  1. proc open(filename: string; mode: FileMode = fmRead; bufSize: int = -1): File {.
  2. ...raises: [IOError], tags: [], forbids: [].}

Opens a file named filename with given mode.

Default mode is readonly. Raises an IOError if the file could not be opened.

The file handle associated with the resulting File is not inheritable.

Source Edit

  1. proc readAll(file: File): string {....tags: [ReadIOEffect], gcsafe,
  2. raises: [IOError], forbids: [].}

Reads all data from the stream file.

Raises an IO exception in case of an error. It is an error if the current file position is not at the beginning of the file.

Source Edit

  1. proc readBuffer(f: File; buffer: pointer; len: Natural): int {.
  2. ...tags: [ReadIOEffect], gcsafe, raises: [IOError], forbids: [].}

Reads len bytes into the buffer pointed to by buffer. Returns the actual number of bytes that have been read which may be less than len (if not as many bytes are remaining), but not greater. Source Edit

  1. proc readBytes(f: File; a: var openArray[int8 | uint8]; start, len: Natural): int {.
  2. ...tags: [ReadIOEffect], gcsafe.}

Reads len bytes into the buffer a starting at a[start]. Returns the actual number of bytes that have been read which may be less than len (if not as many bytes are remaining), but not greater. Source Edit

  1. proc readChar(f: File): char {....tags: [ReadIOEffect],
  2. raises: [IOError, EOFError], forbids: [].}

Reads a single character from the stream f. Should not be used in performance sensitive code. Source Edit

  1. proc readChars(f: File; a: var openArray[char]): int {....tags: [ReadIOEffect],
  2. gcsafe, raises: [IOError], forbids: [].}

Reads up to a.len bytes into the buffer a. Returns the actual number of bytes that have been read which may be less than a.len (if not as many bytes are remaining), but not greater. Source Edit

  1. proc readChars(f: File; a: var openArray[char]; start, len: Natural): int {.
  2. ...tags: [ReadIOEffect], gcsafe, deprecated: "use other `readChars` overload, possibly via: readChars(toOpenArray(buf, start, len-1))",
  3. raises: [IOError], forbids: [].}

Deprecated: use other `readChars` overload, possibly via: readChars(toOpenArray(buf, start, len-1))

Reads len bytes into the buffer a starting at a[start]. Returns the actual number of bytes that have been read which may be less than len (if not as many bytes are remaining), but not greater. Source Edit

  1. proc readFile(filename: string): string {....tags: [ReadIOEffect], gcsafe,
  2. raises: [IOError], forbids: [].}

Opens a file named filename for reading, calls readAll and closes the file afterwards. Returns the string. Raises an IO exception in case of an error. If you need to call this inside a compile time macro you can use staticRead. Source Edit

  1. proc readLine(f: File): string {....tags: [ReadIOEffect], gcsafe,
  2. raises: [IOError, EOFError], forbids: [].}

Reads a line of text from the file f. May throw an IO exception. A line of text may be delimited by LF or CRLF. The newline character(s) are not part of the returned string. Source Edit

  1. proc readLine(f: File; line: var string): bool {....tags: [ReadIOEffect], gcsafe,
  2. raises: [IOError], forbids: [].}

Reads a line of text from the file f into line. May throw an IO exception. A line of text may be delimited by LF or CRLF. The newline character(s) are not part of the returned string. Returns false if the end of the file has been reached, true otherwise. If false is returned line contains no new data. Source Edit

  1. proc readLines(filename: string; n: Natural): seq[string] {.
  2. ...raises: [IOError, EOFError], tags: [ReadIOEffect], forbids: [].}

Reads n lines from the file named filename. Raises an IO exception in case of an error. Raises EOF if file does not contain at least n lines. Available at compile time. A line of text may be delimited by LF or CRLF. The newline character(s) are not part of the returned strings. Source Edit

  1. proc reopen(f: File; filename: string; mode: FileMode = fmRead): bool {.
  2. ...tags: [], gcsafe, raises: [], forbids: [].}

Reopens the file f with given filename and mode. This is often used to redirect the stdin, stdout or stderr file variables.

Default mode is readonly. Returns true if the file could be reopened.

The file handle associated with f won’t be inheritable.

Source Edit

  1. proc setFilePos(f: File; pos: int64; relativeTo: FileSeekPos = fspSet) {....gcsafe,
  2. sideEffect, ...raises: [IOError], tags: [], forbids: [].}

Sets the position of the file pointer that is used for read/write operations. The file’s first byte has the index zero. Source Edit

  1. proc setInheritable(f: FileHandle; inheritable: bool): bool {....raises: [],
  2. tags: [], forbids: [].}

control whether a file handle can be inherited by child processes. Returns true on success. This requires the OS file handle, which can be retrieved via getOsFileHandle.

This procedure is not guaranteed to be available for all platforms. Test for availability with declared() <system.html#declared,untyped>.

Source Edit

  1. proc setStdIoUnbuffered() {....tags: [], gcsafe, raises: [], forbids: [].}

Configures stdin, stdout and stderr to be unbuffered. Source Edit

  1. proc write(f: File; a: varargs[string, `$`]) {....tags: [WriteIOEffect], gcsafe,
  2. raises: [IOError], forbids: [].}

Source Edit

  1. proc write(f: File; b: bool) {....tags: [WriteIOEffect], gcsafe, raises: [IOError],
  2. forbids: [].}

Source Edit

  1. proc write(f: File; c: char) {....tags: [WriteIOEffect], gcsafe, raises: [],
  2. forbids: [].}

Source Edit

  1. proc write(f: File; c: cstring) {....tags: [WriteIOEffect], gcsafe,
  2. raises: [IOError], forbids: [].}

Writes a value to the file f. May throw an IO exception. Source Edit

  1. proc write(f: File; i: BiggestInt) {....tags: [WriteIOEffect], gcsafe,
  2. raises: [IOError], forbids: [].}

Source Edit

  1. proc write(f: File; i: int) {....tags: [WriteIOEffect], gcsafe, raises: [IOError],
  2. forbids: [].}

Source Edit

  1. proc write(f: File; r: BiggestFloat) {....tags: [WriteIOEffect], gcsafe,
  2. raises: [IOError], forbids: [].}

Source Edit

  1. proc write(f: File; r: float32) {....tags: [WriteIOEffect], gcsafe,
  2. raises: [IOError], forbids: [].}

Source Edit

  1. proc write(f: File; s: string) {....tags: [WriteIOEffect], gcsafe,
  2. raises: [IOError], forbids: [].}

Source Edit

  1. proc writeBuffer(f: File; buffer: pointer; len: Natural): int {.
  2. ...tags: [WriteIOEffect], gcsafe, raises: [IOError], forbids: [].}

Writes the bytes of buffer pointed to by the parameter buffer to the file f. Returns the number of actual written bytes, which may be less than len in case of an error. Source Edit

  1. proc writeBytes(f: File; a: openArray[int8 | uint8]; start, len: Natural): int {.
  2. ...tags: [WriteIOEffect], gcsafe.}

Writes the bytes of a[start..start+len-1] to the file f. Returns the number of actual written bytes, which may be less than len in case of an error. Source Edit

  1. proc writeChars(f: File; a: openArray[char]; start, len: Natural): int {.
  2. ...tags: [WriteIOEffect], gcsafe, raises: [IOError], forbids: [].}

Writes the bytes of a[start..start+len-1] to the file f. Returns the number of actual written bytes, which may be less than len in case of an error. Source Edit

  1. proc writeFile(filename, content: string) {....tags: [WriteIOEffect], gcsafe,
  2. raises: [IOError], forbids: [].}

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. Source Edit

  1. proc writeFile(filename: string; content: openArray[byte]) {....raises: [IOError],
  2. tags: [WriteIOEffect], forbids: [].}

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. Source Edit

  1. proc writeLine[Ty](f: File; x: varargs[Ty, `$`]) {.inline,
  2. ...tags: [WriteIOEffect], gcsafe.}

Writes the values x to f and then writes “\n”. May throw an IO exception. Source Edit

Iterators

  1. iterator lines(f: File): string {....tags: [ReadIOEffect], raises: [IOError],
  2. forbids: [].}

Iterates over any line in the file f.

The trailing newline character(s) are removed from the iterated lines.

Example:

  1. proc countZeros(filename: File): tuple[lines, zeros: int] =
  2. for line in filename.lines:
  3. for letter in line:
  4. if letter == '0':
  5. result.zeros += 1
  6. result.lines += 1

Source Edit

  1. iterator lines(filename: string): string {....tags: [ReadIOEffect],
  2. raises: [IOError, IOError], forbids: [].}

Iterates over any line in the file named filename.

If the file does not exist IOError is raised. The trailing newline character(s) are removed from the iterated lines. Example:

Example:

  1. import std/strutils
  2. proc transformLetters(filename: string) =
  3. var buffer = ""
  4. for line in filename.lines:
  5. buffer.add(line.replace("a", "0") & '\n')
  6. writeFile(filename, buffer)

Source Edit

Templates

  1. template `&=`(f: File; x: typed)

An alias for write. Source Edit

  1. template readLines(filename: string): seq[string] {.
  2. ...deprecated: "use readLines with two arguments".}

Deprecated: use readLines with two arguments

Source Edit

  1. template stdmsg(): File

Template which expands to either stdout or stderr depending on useStdoutAsStdmsg compile-time switch. Source Edit