# asyncfile

- マニュアル
- 標準ライブラリ
- 索引

検索
分類:

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()

# インポート

asyncdispatch, os, winlean

# 型

    AsyncFile = ref object
      fd: AsyncFD
      offset: int64

  ソース 編集

# プロシージャ

    proc getFileSize(f: AsyncFile): int64 {...}{.raises: [OSError], tags: [].}

  Retrieves the specified file's size. ソース 編集

    proc newAsyncFile(fd: AsyncFD): AsyncFile {...}{.raises: [Exception, OSError],
                                            tags: [RootEffect].}

  Creates `AsyncFile` with a previously opened file descriptor `fd`. ソース 編集

    proc openAsync(filename: string; mode = fmRead): AsyncFile {...}{.
        raises: [OSError, Exception], tags: [RootEffect].}

  Opens a file specified by the path in `filename` using the specified FileMode `mode` asynchronously. ソース 編集

    proc readBuffer(f: AsyncFile; buf: pointer; size: int): Future[int] {...}{.
        raises: [ValueError, FutureError, Exception], 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 zero is returned and no bytes are read into `buf`

  ソース 編集

    proc read(f: AsyncFile; size: int): Future[string] {...}{.
        raises: [ValueError, FutureError, Exception], 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.

  ソース 編集

    proc readLine(f: AsyncFile): Future[string] {...}{.
        raises: [Exception, ValueError, FutureError], tags: [RootEffect].}

  Reads a single line from the specified file asynchronously. ソース 編集

    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. ソース 編集

    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. ソース 編集

    proc readAll(f: AsyncFile): Future[string] {...}{.
        raises: [Exception, ValueError, FutureError], tags: [RootEffect].}

  Reads all data from the specified file. ソース 編集

    proc writeBuffer(f: AsyncFile; buf: pointer; size: int): Future[void] {...}{.
        raises: [FutureError, Exception], tags: [RootEffect].}

  Writes `size` bytes from `buf` to the file specified asynchronously.

  The returned Future will complete once all data has been written to the specified file.

  ソース 編集

    proc write(f: AsyncFile; data: string): Future[void] {...}{.
        raises: [FutureError, Exception], tags: [RootEffect].}

  Writes `data` to the file specified asynchronously.

  The returned Future will complete once all data has been written to the specified file.

  ソース 編集

    proc setFileSize(f: AsyncFile; length: int64) {...}{.raises: [OSError], tags: [].}

  Set a file length. ソース 編集

    proc close(f: AsyncFile) {...}{.raises: [Exception, OSError], tags: [RootEffect].}

  Closes the file specified. ソース 編集

    proc writeFromStream(f: AsyncFile; fs: FutureStream[string]): owned(Future[void]) {...}{.
        raises: [Exception, FutureError], tags: [RootEffect].}

  Reads data from the specified future stream until it is completed. The data which is read is written to the file immediately and freed from memory.

  This procedure is perfect for saving streamed data to a file without wasting memory.

  ソース 編集

    proc readToStream(f: AsyncFile; fs: FutureStream[string]): owned(Future[void]) {...}{.
        raises: [Exception, FutureError], tags: [RootEffect].}

  Writes data to the specified future stream as the file is read. ソース 編集