On this page
std/syncio
Source EditThis module implements various synchronized I/O operations.
Imports
Types
-
FileHandle = cint
- type that represents an OS file handle; this is useful for low-level file access Source Edit
-
FileMode = enum fmRead, ## Open the file for read access only. ## If the file does not exist, it will not ## be created. 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. If the file does not exist, it ## will be created.
- The file mode when opening a file. Source Edit
Vars
Procs
-
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. UsegetOsFileHandle
instead. Source Edit -
proc open(f: var File; filehandle: FileHandle; mode: FileMode = fmRead): bool {. ...tags: [], raises: [], gcsafe, forbids: [].}
-
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(f: var File; filename: string; mode: FileMode = fmRead; bufSize: int = -1): bool {....tags: [], raises: [], gcsafe, forbids: [].}
-
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 open(filename: string; mode: FileMode = fmRead; bufSize: int = -1): File {. ...raises: [IOError], tags: [], forbids: [].}
-
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 readBuffer(f: File; buffer: pointer; len: Natural): int {. ...tags: [ReadIOEffect], gcsafe, raises: [IOError], forbids: [].}
-
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.}
-
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]): int {....tags: [ReadIOEffect], gcsafe, raises: [IOError], forbids: [].}
-
Reads up to
a.len
bytes into the buffera
. Returns the actual number of bytes that have been read which may be less thana.len
(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, deprecated: "use other `readChars` overload, possibly via: readChars(toOpenArray(buf, start, len-1))", raises: [IOError], forbids: [].}
-
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
Reads -
proc readFile(filename: string): string {....tags: [ReadIOEffect], gcsafe, 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 -
proc readLine(f: File): string {....tags: [ReadIOEffect], gcsafe, 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 byLF
orCRLF
. The newline character(s) are not part of the returned string. Source Edit -
proc readLine(f: File; line: var string): bool {....tags: [ReadIOEffect], gcsafe, raises: [IOError], forbids: [].}
-
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 readLines(filename: string; n: Natural): seq[string] {. ...raises: [IOError, EOFError], tags: [ReadIOEffect], forbids: [].}
-
Reads
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 -
proc reopen(f: File; filename: string; mode: FileMode = fmRead): bool {. ...tags: [], gcsafe, raises: [], forbids: [].}
-
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 setInheritable(f: FileHandle; inheritable: bool): bool {....raises: [], 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
Source Editdeclared() <system.html#declared,untyped>
. -
proc write(f: File; a: varargs[string, `$`]) {....tags: [WriteIOEffect], gcsafe, raises: [IOError], forbids: [].}
- Source Edit
-
proc write(f: File; b: bool) {....tags: [WriteIOEffect], gcsafe, raises: [IOError], forbids: [].}
- Source Edit
-
proc write(f: File; c: char) {....tags: [WriteIOEffect], gcsafe, raises: [], forbids: [].}
- Source Edit
-
proc write(f: File; c: cstring) {....tags: [WriteIOEffect], gcsafe, raises: [IOError], forbids: [].}
-
Writes a value to the file
f
. May throw an IO exception. Source Edit -
proc write(f: File; i: BiggestInt) {....tags: [WriteIOEffect], gcsafe, raises: [IOError], forbids: [].}
- Source Edit
-
proc write(f: File; i: int) {....tags: [WriteIOEffect], gcsafe, raises: [IOError], forbids: [].}
- Source Edit
-
proc write(f: File; r: BiggestFloat) {....tags: [WriteIOEffect], gcsafe, raises: [IOError], forbids: [].}
- Source Edit
-
proc writeBuffer(f: File; buffer: pointer; len: Natural): int {. ...tags: [WriteIOEffect], gcsafe, raises: [IOError], forbids: [].}
-
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 writeChars(f: File; a: openArray[char]; start, len: Natural): int {. ...tags: [WriteIOEffect], gcsafe, raises: [IOError], forbids: [].}
-
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
Iterators
-
iterator lines(f: File): string {....tags: [ReadIOEffect], raises: [IOError], forbids: [].}
-
Iterates over any line in the file
f
.The trailing newline character(s) are removed from the iterated lines.
Example:
Source Editproc 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
-
iterator lines(filename: string): string {....tags: [ReadIOEffect], 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:
Source Editimport std/strutils proc transformLetters(filename: string) = var buffer = "" for line in filename.lines: buffer.add(line.replace("a", "0") & '\n') writeFile(filename, buffer)
Templates
© 2006–2024 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/syncio.html