file_streams/file_open_mode
Types
The modes that can be specified when opening a file stream with
file_stream.open().
pub type FileOpenMode {
Append
Binary
DelayedWrite(size: Int, delay: Int)
Encoding(encoding: TextEncoding)
Exclusive
Read
ReadAhead(size: Int)
Write
}
Constructors
-
AppendThe file is opened for writing. It is created if it does not exist. Every write operation to a file opened with
Appendtakes place at the end of the file. -
BinaryCauses read operations on the file stream to return binaries rather than lists.
If this mode is specified then binary data can be read from and/or written to the file stream. If it is not specified then text data data can be read from and/or written to the file stream.
This mode must not be present when the
Encoding(...)mode is present. -
DelayedWrite(size: Int, delay: Int)Data in subsequent
file_stream.write_*calls are buffered until at leastsizebytes are buffered, or until the oldest buffered data isdelaymilliseconds old. Then all buffered data is written in one operating system call. The buffered data is also flushed before some other file operations that are notfile_stream.write_*calls.The purpose of this option is to increase performance by reducing the number of operating system calls. Thus,
file_stream.write_*calls must be for sizes significantly less thansize, and should not interspersed by too many other file operations.When this option is used, the result of
file_stream.write_*calls can prematurely be reported as successful, and if a write error occurs, the error is reported as the result of the next file operation, which is not executed.For example, when
DelayedWriteis used, after a number offile_stream.write_*calls,file_stream.close()can returnError(FileStreamError(Enospc)))as there is not enough space on the device for previously written data.file_stream.close()must be called again, as the file is still open. -
Encoding(encoding: TextEncoding)Makes the file stream perform automatic translation of text to and from the specified text encoding when using the
file_stream.read_line(),file_stream.read_chars(), andfile_stream.write_chars()functions.If characters are written that can’t be converted to the specified encoding then an error occurs and the file is closed.
-
ExclusiveThe file is opened for writing. It is created if it does not exist. If the file exists,
Error(FileStreamError(Eexist))is returned byfile_stream.open().This option does not guarantee exclusiveness on file systems not supporting
O_EXCLproperly, such as NFS. Do not depend on this option unless you know that the file system supports it (in general, local file systems are safe). -
ReadThe file, which must exist, is opened for reading.
-
ReadAhead(size: Int)Activates read data buffering. If
file_stream.read_*calls are for significantly less thansizebytes, read operations to the operating system are still performed for blocks ofsizebytes. The extra data is buffered and returned in subsequentfile_stream.read_*calls, giving a performance gain as the number of operating system calls is reduced.If
file_stream.read_*calls are for sizes not significantly less thansizebytes, or are greater thansizebytes, no performance gain can be expected. -
WriteThe file is opened for writing. It is created if it does not exist. If the file exists and
Writeis not combined withRead, the file is truncated.