Specifies in what mode a file should be opened. These flags can be combined.
Default mode. Not very useful.
Opens an existing file. Unless combined with create, fails if the file does not exist.
Creates a new file. Fails if the file is opened without write access. Fails if the file already exists and not combined with truncate or open.
Opens the file if it already exists or creates it if it does not.
Allows only appending to the end of the file. Seek operations only affect subsequent reads. Upon writing, the file pointer gets set to the end of the file. Requires write access to the file.
Truncates the file. This has no effect if the file has been created anew. Requires write access to the file.
Specifies what other processes are allowed to do to the file. These flags can be combined.
File flags that determine how a file stream is created and used.
Since all methods in this struct are pure, the high-level configuration flags given here are converted to the platform-specific file flags at compile time.
// Creates the file "foobar", truncates it, and opens it in write-only mode auto f = File("foobar", FileFlags.writeEmpty);
An existing file is opened with read access. This is likely the most commonly used set of flags.
An existing file is opened with write access.
A new file is created with write access. Fails if the file already exists.
A new file is either opened or created with write access.
A new file is either opened or created, truncated if necessary, with write access. This ensures that an empty file is opened.
An existing file is opened with read/write access.
A new file is created with read/write access. Fails if the file already exists.
A new file is either opened or created with read/write access.
A new file is either opened or created, truncated if necessary, with read/write access. This ensures that an empty file is opened.
Constructs the FileFlag with the given mode, access, and share attributes. These high-level flags are converted to the equivalent platform-specific flags that are needed when opening the file.
Mode mode | Mode the file should be opened in. That is, to open, create, append, or truncate the file. |
Access access | The permissions on the file stream. That is, read access, write access, or both. |
Share share | The sharing permissions other processes are allowed on the file stream when trying to open the same file. By default, other processes are prevented from opening the file if they request read, write, or delete access. If you wish to allow other processes to read the file while it is open in this process, set this to Share.read. Currently only used by Windows. |
immutable flags = FileFlags(Mode.open, Access.read);
Constructs the file flags from a mode string.
This simply calls FileFlags.parse.
FileFlags ff = "wb+"; assert(ff == FileFlags.readWriteEmpty); assert(ff == FileFlags("wb+"));
Parses an fopen-style mode string such as "r+". All possible mode strings include:
Mode String | Meaning |
---|---|
"wb" | Write truncated |
"wb+" | Read/write truncated |
"w+b" | Read/write truncated |
"wbx" | Write new |
"wb+x" | Read/write new |
"w+bx" | Read/write new |
"rb" | Read existing" |
"rb+" | Read/write existing" |
"r+b" | Read/write existing" |
"ab" | Append new" |
"ab+" | Append/read new" |
"a+b" | Append/read new" |
static assert(FileFlags("wb") == FileFlags.writeEmpty); static assert(FileFlags("wb+") == FileFlags.readWriteEmpty); static assert(FileFlags("w+b") == FileFlags.readWriteEmpty); static assert(FileFlags("wbx") == FileFlags.writeNew); static assert(FileFlags("wb+x") == FileFlags.readWriteNew); static assert(FileFlags("w+bx") == FileFlags.readWriteNew); static assert(FileFlags("rb") == FileFlags.readExisting); static assert(FileFlags("rb+") == FileFlags.readWriteExisting); static assert(FileFlags("r+b") == FileFlags.readWriteExisting); static assert(FileFlags("ab") == FileFlags(Mode.openOrCreate | Mode.append, Access.write)); static assert(FileFlags("ab+") == FileFlags(Mode.openOrCreate | Mode.append, Access.readWrite)); static assert(FileFlags("a+b") == FileFlags(Mode.openOrCreate | Mode.append, Access.readWrite));