// Open a new file in read/write mode. Throws an exception if the file exists. auto f = File("myfile", FileFlags.readWriteNew); // Write an arbitrary array to the stream. f.write("Hello world!"); // Seek to the beginning. f.position = 0; // Read in 5 bytes. char buf[5]; f.read(buf); assert(buf == "Hello");
A cross-platform wrapper around low-level file operations.
Platform-specific file handle. On Posix systems, this is the file descriptor
int. On Windows, this is a
HANDLE.
Platform-specific file handle. On Posix systems, this is the file descriptor
int. On Windows, this is a
HANDLE.
Opens or creates a file by name. By default, an existing file is opened in read-only mode.
string name | Path to the file to open. |
FileFlags flags | How to open the file. |
// Create a brand-new file and write to it. Throws an exception if the // file already exists. The file is automatically closed when it falls // out of scope. auto f = File("filename", FileFlags.writeNew); f.write("Hello world!");
Takes control of a file handle.
It is assumed that we have exclusive control over the file handle and will
be closed upon destruction as usual. If non-exclusive control is desired,
use dup instead.
This function is useful in a couple of situations:
Handle h | The handle to assume control over. For Posix, this is a file descriptor (int). For Windows, this is an object handle ( HANDLE). |
Duplicates the given platform-specific file handle. This is useful for taking non-exclusive control over a file handle.
Closes the stream if it is open. Otherwise, it does nothing.
Returns true if the file is open.
Vectorized read.
ubyte[][] bufs | The buffers to read the data into. The length of each buffer specifies how much data should be read. |
Writes data to the file.
ubyte[] data | The data to write to the file. The length of the slice indicates how much data should be written. |
Vectorized write.
ubyte[][] bufs | The buffers to write the data from. The length of each buffer specifies how much data should be written. |
Seeks relative to a position.
long offset | Offset relative to a reference point. |
From from | Optional reference point. |
Gets the size of the file.
auto f = File("foobar", FileFlags.writeEmpty); f.write("test"); assert(f.length == 4);
Checks if the file refers to a terminal.
assert(stdin.isTerminal); assert(stderr.isTerminal); assert(stdout.isTerminal); assert(!File("test", FileFlags.writeAlways).isTerminal);
Like lock, but returns false immediately if the lock is held by another process. Returns true if the specified region in the file was successfully locked.
Syncs all modified cached data of the file to disk. This includes data written to the file as well as meta data (e.g., last modified time, last access time).