This module implements asynchronous file reading and writing.
import asyncfile, asyncdispatch, os proc main() {.async.} = var file = openAsync(getTempDir() / "foobar.txt", fmReadWrite) await file.write("test") file.setFilePos(0) let data = await file.readAll() doAssert data == "test" file.close() waitFor main()
Procs
proc openAsync(filename: string; mode = fmRead): AsyncFile {.raises: [OSError], tags: [ReadDirEffect].}
- Opens a file specified by the path in filename using the specified mode asynchronously. Source
proc read(f: AsyncFile; size: int): Future[string] {.raises: [Exception, FutureError], tags: [RootEffect].}
-
Read size bytes from the specified file asynchronously starting at the current position of the file pointer.
If the file pointer is past the end of the file then an empty string is returned.
Source proc readLine(f: AsyncFile): Future[string] {.raises: [FutureError], tags: [RootEffect].}
- Reads a single line from the specified file asynchronously. Source
proc getFilePos(f: AsyncFile): int64 {.raises: [], tags: [].}
- Retrieves the current position of the file pointer that is used to read from the specified file. The file's first byte has the index zero. Source
proc setFilePos(f: AsyncFile; pos: int64) {.raises: [], 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
proc readAll(f: AsyncFile): Future[string] {.raises: [FutureError], tags: [RootEffect].}
- Reads all data from the specified file. Source
proc write(f: AsyncFile; data: string): Future[void] {. raises: [Exception, FutureError], tags: [RootEffect].}
-
Writes data to the file specified asynchronously.
The returned Future will complete once all data has been written to the specified file.
Source proc close(f: AsyncFile) {.raises: [OSError], tags: [].}
- Closes the file specified. Source