io
This is a part of system.nim
, you should not manually import it.
Imports
Types
File = ptr CFile
- The type representing a file handle. Source Edit
FileMode = enum fmRead, ## Open the file for read access only. fmWrite, ## Open the file for write access only. ## If the file does not exist, it will be ## created. Existing files will be cleared! fmReadWrite, ## Open the file for read and write access. ## If the file does not exist, it will be ## created. Existing files will be cleared! fmReadWriteExisting, ## Open the file for read and write access. ## If the file does not exist, it will not be ## created. The existing file will not be cleared. fmAppend ## Open the file for writing only; append data ## at the end.
- The file mode when opening a file. Source Edit
FileHandle = cint
- type that represents an OS file handle; this is useful for low-level file access Source Edit
Vars
stdin: File
- The standard input stream. Source Edit
stdout: File
- The standard output stream. Source Edit
stderr: File
- The standard error stream. Source Edit
Procs
proc readBuffer(f: File; buffer: pointer; len: Natural): int {...}{. tags: [ReadIOEffect], gcsafe, locks: 0, raises: [IOError].}
- reads
len
bytes into the buffer pointed to bybuffer
. Returns the actual number of bytes that have been read which may be less thanlen
(if not as many bytes are remaining), but not greater. Source Edit proc readBytes(f: File; a: var openArray[int8 | uint8]; start, len: Natural): int {...}{. tags: [ReadIOEffect], gcsafe, locks: 0.}
- reads
len
bytes into the buffera
starting ata[start]
. Returns the actual number of bytes that have been read which may be less thanlen
(if not as many bytes are remaining), but not greater. Source Edit proc readChars(f: File; a: var openArray[char]; start, len: Natural): int {...}{. tags: [ReadIOEffect], gcsafe, locks: 0, raises: [IOError].}
-
reads
len
bytes into the buffera
starting ata[start]
. Returns the actual number of bytes that have been read which may be less thanlen
(if not as many bytes are remaining), but not greater.Warning: The buffer
Source Edita
must be pre-allocated. This can be done using, for example,newString
. proc write(f: File; c: cstring) {...}{.tags: [WriteIOEffect], gcsafe, locks: 0, raises: [IOError].}
- Writes a value to the file
f
. May throw an IO exception. Source Edit proc writeBuffer(f: File; buffer: pointer; len: Natural): int {...}{. tags: [WriteIOEffect], gcsafe, locks: 0, raises: [IOError].}
- writes the bytes of buffer pointed to by the parameter
buffer
to the filef
. Returns the number of actual written bytes, which may be less thanlen
in case of an error. Source Edit proc writeBytes(f: File; a: openArray[int8 | uint8]; start, len: Natural): int {...}{. tags: [WriteIOEffect], gcsafe, locks: 0.}
- writes the bytes of
a[start..start+len-1]
to the filef
. Returns the number of actual written bytes, which may be less thanlen
in case of an error. Source Edit proc writeChars(f: File; a: openArray[char]; start, len: Natural): int {...}{. tags: [WriteIOEffect], gcsafe, locks: 0, raises: [IOError].}
- writes the bytes of
a[start..start+len-1]
to the filef
. Returns the number of actual written bytes, which may be less thanlen
in case of an error. Source Edit proc write(f: File; s: string) {...}{.tags: [WriteIOEffect], gcsafe, locks: 0, raises: [IOError].}
- Source Edit
proc close(f: File) {...}{.tags: [], gcsafe, raises: [].}
- Closes the file. Source Edit
proc readChar(f: File): char {...}{.tags: [ReadIOEffect], raises: [IOError, EOFError].}
- Reads a single character from the stream
f
. Should not be used in performance sensitive code. Source Edit proc flushFile(f: File) {...}{.tags: [WriteIOEffect], raises: [].}
- Flushes
f
's buffer. Source Edit proc getFileHandle(f: File): FileHandle {...}{.raises: [], tags: [].}
- 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. UsegetOsFileHandle
instead. Source Edit proc getOsFileHandle(f: File): FileHandle {...}{.raises: [], tags: [].}
- returns the OS file handle of the file
f
. This is only useful for platform specific programming. Source Edit proc setInheritable(f: FileHandle; inheritable: bool): bool {...}{.raises: [], tags: [].}
-
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
Source Editdeclared() <system.html#declared,untyped>
. proc readLine(f: File; line: var TaintedString): bool {...}{.tags: [ReadIOEffect], gcsafe, locks: 0, raises: [IOError].}
- reads a line of text from the file
f
intoline
. May throw an IO exception. A line of text may be delimited byLF
orCRLF
. The newline character(s) are not part of the returned string. Returnsfalse
if the end of the file has been reached,true
otherwise. Iffalse
is returnedline
contains no new data. Source Edit proc readLine(f: File): TaintedString {...}{.tags: [ReadIOEffect], gcsafe, locks: 0, raises: [IOError, EOFError].}
- reads a line of text from the file
f
. May throw an IO exception. A line of text may be delimited byLF
orCRLF
. The newline character(s) are not part of the returned string. Source Edit proc write(f: File; i: int) {...}{.tags: [WriteIOEffect], gcsafe, locks: 0, raises: [IOError].}
- Source Edit
proc write(f: File; i: BiggestInt) {...}{.tags: [WriteIOEffect], gcsafe, locks: 0, raises: [IOError].}
- Source Edit
proc write(f: File; b: bool) {...}{.tags: [WriteIOEffect], gcsafe, locks: 0, raises: [IOError].}
- Source Edit
proc write(f: File; r: float32) {...}{.tags: [WriteIOEffect], gcsafe, locks: 0, raises: [IOError].}
- Source Edit
proc write(f: File; r: BiggestFloat) {...}{.tags: [WriteIOEffect], gcsafe, locks: 0, raises: [IOError].}
- Source Edit
proc write(f: File; c: char) {...}{.tags: [WriteIOEffect], gcsafe, locks: 0, raises: [].}
- Source Edit
proc write(f: File; a: varargs[string, `$`]) {...}{.tags: [WriteIOEffect], gcsafe, locks: 0, raises: [IOError].}
- Source Edit
proc endOfFile(f: File): bool {...}{.tags: [], gcsafe, locks: 0, raises: [].}
- Returns true if
f
is at the end. Source Edit proc readAll(file: File): TaintedString {...}{.tags: [ReadIOEffect], gcsafe, locks: 0, raises: [IOError].}
-
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 proc writeLine[Ty](f: File; x: varargs[Ty, `$`]) {...}{.inline, tags: [WriteIOEffect], gcsafe, locks: 0.}
- writes the values
x
tof
and then writes "\n". May throw an IO exception. Source Edit proc open(f: var File; filename: string; mode: FileMode = fmRead; bufSize: int = -1): bool {...}{.tags: [], raises: [], gcsafe, locks: 0.}
-
Opens a file named
filename
with givenmode
.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
Source EditFile
is not inheritable. proc reopen(f: File; filename: string; mode: FileMode = fmRead): bool {...}{. tags: [], gcsafe, locks: 0, raises: [].}
-
reopens the file
f
with givenfilename
andmode
. This is often used to redirect thestdin
,stdout
orstderr
file variables.Default mode is readonly. Returns true if the file could be reopened.
The file handle associated with
Source Editf
won't be inheritable. proc open(f: var File; filehandle: FileHandle; mode: FileMode = fmRead): bool {...}{. tags: [], raises: [], gcsafe, locks: 0.}
-
Creates a
File
from afilehandle
with givenmode
.Default mode is readonly. Returns true if the file could be opened.
The passed file handle will no longer be inheritable.
Source Edit proc open(filename: string; mode: FileMode = fmRead; bufSize: int = -1): File {...}{. raises: [IOError], tags: [].}
-
Opens a file named
filename
with givenmode
.Default mode is readonly. Raises an
IOError
if the file could not be opened.The file handle associated with the resulting
Source EditFile
is not inheritable. proc setFilePos(f: File; pos: int64; relativeTo: FileSeekPos = fspSet) {...}{.gcsafe, locks: 0, raises: [IOError], tags: [].}
- 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
proc getFilePos(f: File): int64 {...}{.gcsafe, locks: 0, raises: [IOError], tags: [].}
- 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 proc getFileSize(f: File): int64 {...}{.tags: [ReadIOEffect], gcsafe, locks: 0, raises: [IOError].}
- retrieves the file size (in bytes) of
f
. Source Edit proc setStdIoUnbuffered() {...}{.tags: [], gcsafe, locks: 0, raises: [].}
- Configures
stdin
,stdout
andstderr
to be unbuffered. Source Edit proc readFile(filename: string): TaintedString {...}{.tags: [ReadIOEffect], gcsafe, locks: 0, raises: [IOError].}
- 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 proc writeFile(filename, content: string) {...}{.tags: [WriteIOEffect], gcsafe, locks: 0, raises: [IOError].}
- Opens a file named
filename
for writing. Then writes thecontent
completely to the file and closes the file afterwards. Raises an IO exception in case of an error. Source Edit proc writeFile(filename: string; content: openArray[byte]) {...}{.raises: [IOError], tags: [WriteIOEffect].}
- Opens a file named
filename
for writing. Then writes thecontent
completely to the file and closes the file afterwards. Raises an IO exception in case of an error. Source Edit proc readLines(filename: string; n: Natural): seq[TaintedString] {...}{. raises: [IOError, EOFError], tags: [ReadIOEffect].}
- read
n
lines from the file namedfilename
. Raises an IO exception in case of an error. Raises EOF if file does not contain at leastn
lines. Available at compile time. A line of text may be delimited byLF
orCRLF
. The newline character(s) are not part of the returned strings. Source Edit
Iterators
iterator lines(filename: string): TaintedString {...}{.tags: [ReadIOEffect], raises: [IOError, IOError].}
-
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:import strutils proc transformLetters(filename: string) = var buffer = "" for line in filename.lines: buffer.add(line.replace("a", "0") & '\x0A') writeFile(filename, buffer)
Source Edit iterator lines(f: File): TaintedString {...}{.tags: [ReadIOEffect], raises: [IOError].}
-
Iterate over any line in the file
f
.The trailing newline character(s) are removed from the iterated lines. Example:
proc countZeros(filename: File): tuple[lines, zeros: int] = for line in filename.lines: for letter in line: if letter == '0': result.zeros += 1 result.lines += 1
Source Edit
Templates
template stdmsg(): File
- Template which expands to either stdout or stderr depending on
useStdoutAsStdmsg
compile-time switch. Source Edit template readLines(filename: string): seq[TaintedString] {...}{. deprecated: "use readLines with two arguments".}
- Source Edit
© 2006–2021 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/io.html