Janet 1.38.0-73334f3 Documentation
(Other Versions: 1.37.1 1.36.0 1.35.0 1.34.0 1.31.0 1.29.1 1.28.0 1.27.0 1.26.0 1.25.1 1.24.0 1.23.0 1.22.0 1.21.0 1.20.0 1.19.0 1.18.1 1.17.1 1.16.1 1.15.0 1.13.1 1.12.2 1.11.1 1.10.1 1.9.1 1.8.1 1.7.0 1.6.0 1.5.1 1.5.0 1.4.0 1.3.1 )

File Module

Index

file/close file/flush file/lines file/open file/read file/seek file/tell file/temp file/write


file/close cfunction source

(file/close f)

Close a file and release all related resources. When you are done reading a file, close it to prevent a resource leak and let other processes read the file.

Community Examples

file/flush cfunction source

(file/flush f)

Flush any buffered bytes to the file system. In most files, writes are buffered for efficiency reasons. Returns the file handle.

Community Examples

file/lines function source

(file/lines file)

Return an iterator over the lines of a file.

Community Examples

file/open cfunction source

(file/open path &opt mode buffer-size)

Open a file. path is an absolute or relative path, and mode is a set of flags indicating the mode to open the file in. mode is a keyword where each character represents a flag. If the file cannot be opened, returns nil, otherwise returns the new file handle. Mode flags:

  • r - allow reading from the file

  • w - allow writing to the file

  • a - append to the file

Following one of the initial flags, 0 or more of the following flags can be appended:

  • b - open the file in binary mode (rather than text mode)

  • + - append to the file instead of overwriting it

  • n - error if the file cannot be opened instead of returning nil

See fopen (<stdio.h>, C99) for further details.

Community Examples

file/read cfunction source

(file/read f what &opt buf)

Read a number of bytes from a file f into a buffer. A buffer buf can be provided as an optional third argument, otherwise a new buffer is created. what can either be an integer or a keyword. Returns the buffer with file contents. Values for what:

  • :all - read the whole file

  • :line - read up to and including the next newline character

  • n (integer) - read up to n bytes from the file

Community Examples

file/seek cfunction source

(file/seek f &opt whence n)

Jump to a relative location in the file f. whence must be one of:

  • :cur - jump relative to the current file location

  • :set - jump relative to the beginning of the file

  • :end - jump relative to the end of the file

By default, whence is :cur. Optionally a value n may be passed for the relative number of bytes to seek in the file. n may be a real number to handle large files of more than 4GB. Returns the file handle.

Community Examples

file/tell cfunction source

(file/tell f)

Get the current value of the file position for file f.

Community Examples

file/temp cfunction source

(file/temp)

Open an anonymous temporary file that is removed on close. Raises an error on failure.

Community Examples

file/write cfunction source

(file/write f bytes)

Writes to a file. 'bytes' must be string, buffer, or symbol. Returns the file.

Community Examples