Constructor taking the name of the file to open and the open mode.
A destructor is present on this object, but not explicitly documented in the source.
A postblit is present on this object, but not explicitly documented in the source.
Returns an input range set up to read from the file handle a chunk at a time.
Returns an input range set up to read from the file handle one line at a time.
Returns an input range set up to read from the file handle one line at a time. Each line will be newly allocated. front will cache its value to allow repeated calls without unnecessary allocations.
If the file is closed or not yet opened, succeeds vacuously. Otherwise, returns $(CSTDIO clearerr) for the file handle.
If the file was closed or not yet opened, succeeds vacuously. Otherwise closes the file (by calling $(CSTDIO fclose)), throwing on error. Even if an exception is thrown, afterwards the File object is empty. This is different from detach in that it always closes the file; consequently, all other File objects referring to the same handle will see a closed file henceforth.
Detaches from the underlying file. If the sole owner, calls close.
First calls detach (throwing on failure), then attempts to associate the given file descriptor with the File, and sets the file's name to null.
Flushes the C FILE buffers.
Returns the FILE* corresponding to this object.
Locks the specified file segment. If the file segment is already locked by another process, waits until the existing lock is released. If both start and length are zero, the entire file is locked.
Returns an output range that locks the file and allows fast writing to it.
Output range which locks the file when created, and unlocks the file when it goes out of scope.
Assigns a file to another. The target of the assignment gets detached from whatever file it was attached to, and attaches itself to the new file.
Detaches from the current file (throwing on failure), and then attempts to open file name with mode stdioOpenmode. The mode has the same semantics as in the C standard library $(CSTDIO fopen) function.
Detaches from the current file (throwing on failure), and then runs a command by calling the C standard library function _popen.
Calls $(CSTDIO fread) for the file handle. The number of items to read and the size of each item is inferred from the size and type of the input array, respectively.
Calls $(CSTDIO fwrite) for the file handle. The number of items to write and the size of each item is inferred from the size and type of the input array, respectively. An error is thrown if the buffer could not be written in its entirety.
Reads formatted data from the file using std._format.formattedRead.
Read line from the file handle and return it as a specified type.
Read line from the file handle and write it to buf[], including terminating character.
Reuses the File object to either open a different file, or change the file mode. If name is null, the mode of the currently open file is changed; otherwise, a new file is opened, reusing the C FILE*. The function has the same semantics as in the C standard library $(CSTDIO freopen) function.
Calls $(CSTDIO rewind) for the file handle.
Calls $(CSTDIO fseek) for the file handle to move its position indicator.
Calls $(CSTDIO setvbuf) for the file handle.
Calls $(CSTDIO setvbuf) for the file handle.
Forces any data buffered by the OS to be written to disk. Call flush before calling this function to flush the C FILE buffers first.
Attempts to lock the specified file segment. If both start and length are zero, the entire file is locked.
Removes the lock over the specified file segment.
First calls detach (throwing on failure), and then attempts to associate the given Windows HANDLE with the File. The mode must be compatible with the access attributes of the handle. Windows only.
Writes its arguments in text format to the file.
Writes its arguments in text format to the file, according to the format string fmt.
Equivalent to file.writef(fmt, args, '\n').
Writes its arguments in text format to the file, followed by a newline.
Returns true if the file is at end (see $(CSTDIO feof)).
If the file is closed or not yet opened, returns true. Otherwise, returns $(CSTDIO ferror) for the file handle.
Returns the file number corresponding to this object.
Returns true if the file is opened.
Returns the name last used to initialize this File, if any.
Returns the size of the file in bytes, ulong.max if file is not searchable or throws if the operation fails.
Calls $(CSTDIO ftell) for the managed file handle, which returns the current value of the position indicator of the file handle.
Returns the underlying operating system HANDLE (Windows only).
Returns a temporary file by calling $(CSTDIO tmpfile). Note that the created file has no name.
Creates an input range set up to parse one line at a time from the file into a tuple.
// test.d import std.stdio; void main(string[] args) { auto f = File("test.txt", "w"); // open for writing f.write("Hello"); if (args.length > 1) { auto g = f; // now g and f write to the same file // internal reference count is 2 g.write(", ", args[1]); // g exits scope, reference count decreases to 1 } f.writeln("!"); // f exits scope, reference count falls to zero, // underlying `FILE*` is closed. }
% rdmd test.d Jimmy % cat test.txt Hello, Jimmy! % __
Encapsulates a FILE*. Generally D does not attempt to provide thin wrappers over equivalent functions in the C standard library, but manipulating FILE* values directly is unsafe and error-prone in many ways. The File type ensures safe manipulation, automatic file closing, and a lot of convenience.
The underlying FILE* handle is maintained in a reference-counted manner, such that as soon as the last File variable bound to a given FILE* goes out of scope, the underlying FILE* is automatically closed.