File
type provides safe operation, automatic file closing and other convenient features. The underlying descriptor is stored with reference counting , so when the last File
variable is out of scope, the descriptor is automatically closed. import core.stdc.errno; import std.exception; import std.stdio; void main(string[] args) { try { // File β , // fopen C. // // r β . . // w β . // , , // . // a β . // , . // . // , . // r+ β ( ). // . // w+ β ( // ). , // , . // a+ β ( ), // . // , // . // , . auto file = File("test.txt", "r"); // , , // . file.close(); // . .: - , // , // Go' defer: // scope(exit) file.close(); } catch (ErrnoException ex) { switch(ex.errno) { case EPERM: case EACCES: // break; case ENOENT: // break; default: // break; } } }
errno
property of this exception.File
type is a wrapper around a C language function, the returned error code will be one of the constants defined in core.stdc.errno
. This is the main way to access files and handle errors. Extended information can be collected using the std.file.getAttributes
function, which returns an unsigned integer. This number contains several bit flags that are set differently depending on the operating system. More information about these flags can be found here . import std.exception; import std.stdio; void main(string[] args) { try { auto file = File("test.txt", "r"); // 10 . file.seek(10, SEEK_SET); // 2 . file.seek(-2, SEEK_CUR); // 4 . file.seek(-4, SEEK_END); // . auto pos = file.tell(); // . file.rewind(); } catch (ErrnoException ex) { // } }
import std.exception; import std.stdio; void main(string[] args) { try { byte[] data = [0x68, 0x65, 0x6c, 0x6c, 0x6f]; auto file = File("test.txt", "w"); file.rawWrite(data); } catch (ErrnoException ex) { // } }
import std.file; void main(string[] args) { try { write("test.txt", [0x68, 0x65, 0x6c, 0x6c, 0x6f]); } catch (FileException ex) { // } }
import std.exception; import std.stdio; void main(string[] args) { try { auto file = File("test.txt", "w"); // . file.write("1: Lorem ipsum\n"); // , . file.writeln("2: Lorem ipsum"); // . file.writef("3: %s", "Lorem ipsum\n"); // , . file.writefln("4: %s", "Lorem ipsum"); } catch (ErrnoException ex) { // } }
import std.file; import std.outbuffer; void main(string[] args) { auto buffer = new OutBuffer(); ubyte[] data = [0x68, 0x65, 0x6c, 0x6c, 0x6f]; buffer.write(data); buffer.write(' '); buffer.write("world"); try { write("test.txt", buffer.toBytes()); } catch (FileException ex) { // } }
import std.exception; import std.stdio; void main(string[] args) { try { byte[] buffer; buffer.length = 1024; auto file = File("test.txt", "r"); auto data = file.rawRead(buffer); } catch (ErrnoException ex) { // } }
rawRead
method fills the buffer and returns a slice of this buffer. The buffer length is the maximum number of bytes that will be read. import std.file; void main(string[] args) { try { auto data = cast(byte[]) read("test.txt"); } catch (FileException ex) { // } }
read
function is used again, but this time with the second parameter specifying the maximum number of bytes to read. If the file is less than the specified limit, only the data from the file will be returned. import std.file; void main(string[] args) { try { auto data = cast(byte[]) read("test.txt", 5); } catch (FileException ex) { // } }
import std.exception; import std.stdio; void main(string[] args) { try { auto file = File("test.txt", "r"); foreach (buffer; file.byChunk(1024)) { // buffer } } catch (ErrnoException ex) { // } }
byChunk
method returns an input byte range that reads only a specified portion of a file descriptor at a time. In this case, each call will return a maximum of 1024 bytes. For each call, the buffer is used again, so if you need to save data between calls, you need to copy it. import std.exception; import std.stdio; void main(string[] args) { try { auto file = File("test.txt", "r"); string line; while ((line = file.readln()) !is null) { // line } } catch (ErrnoException ex) { // } }
readln
function allocates a new buffer to read each line. import std.exception; import std.stdio; void main(string[] args) { try { auto file = File("test.txt", "r"); char[] buffer; while (file.readln(buffer)) { // buffer } } catch (ErrnoException ex) { // } }
import std.exception; import std.stdio; void main(string[] args) { try { auto file = File("test.txt", "r"); foreach (line; file.byLine) { // line } } catch (ErrnoException ex) { // } }
byLine
method returns an input range that reads one line at a time from a file descriptor. With each call, the buffer is used again, so if you need to save data between calls, you must copy it. However, there is a convenient byLineCopy
method that does this automatically. import std.file; import std.utf; void main(string[] args) { try { // UTF8-. auto utf8Data = readText("test.txt"); // UTF16-. auto utf16Data = readText!(wstring)("test.txt"); // utf32-.. auto utf32Data = readText!(dstring)("test.txt"); } catch (UTFException ex) { // } catch (FileException ex) { // } }
File
type structure. If a file with the same name already exists, its contents are deleted, and the file is considered empty. import std.exception; void main(string[] args) { try { File("test.txt", "w"); } catch (ErrnoException ex) { // } }
import std.file; void main(string[] args) { if (exists("test.txt")) { // } }
import std.file; void main(string[] args) { try { rename("source.txt", "destination.txt"); } catch (FileException ex) { // } }
import std.file; void main(string[] args) { try { copy("source.txt", "destination.txt"); } catch (FileException ex) { // } }
import std.file; void main(string[] args) { try { remove("test.txt"); } catch (FileException ex) { // } }
attributes
property. import std.file; import std.stdio : writefln; void main(string[] args) { try { auto file = DirEntry("test.txt"); writefln(" : %s", file.name); writefln(" : %s", file.isDir); writefln(" : %s", file.isFile); writefln(" : %s", file.isSymlink); writefln(" : %s", file.size); writefln(" : %s", file.timeLastAccessed); writefln(" : %s", file.timeLastModified); writefln(": %b", file.attributes); } catch (FileException ex) { // } }
import std.file; void main(string[] args) { auto file = "test.txt"; auto size = 100; try { if (file.exists() && file.isFile()) { write(file, read(file, size)); } } catch (FileException ex) { // } }
import std.file; import std.outbuffer; import std.string; import std.zip; void main(string[] args) { try { auto file = new ArchiveMember(); file.name = "test.txt"; auto data = new OutBuffer(); data.write("Lorem ipsum"); file.expandedData = data.toBytes(); auto zip = new ZipArchive(); zip.addMember(file); write("test.zip", zip.build()); } catch (ZipException ex) { // } }
import std.file; import std.zip; void main(string[] args) { try { auto zip = new ZipArchive(read("test.zip")); foreach (filename, member; zip.directory) { auto data = zip.expand(member); // data } } catch (ZipException ex) { // } }
import std.file; import std.zlib; void main(string[] args) { try { auto data = compress("Lorem ipsum dolor sit amet"); write("test.dat", data); } catch (ZlibException ex) { // } }
std.zlib
module uses the C language Zlib library. import std.file; import std.zlib; void main(string[] args) { try { auto data = uncompress(read("test.dat")); // } catch (ZlibException ex) { // } }
import core.stdc.errno; import core.sys.posix.sys.stat; import std.conv; import std.string; void main(string[] args) { auto file = "test.txt"; auto result = chmod(file.toStringz(), octal!(666)); if (result != 0) { switch(errno) { case EPERM: case EACCES: // break; case ENOENT: // break; default: // break; } } }
chmod
system call works exactly the same as the chmod
from the Unix shell . The file name and its new permissions (expressed as an octal number) are indicated. To modify a file this way, you need permissions on the operation itself. To do this, you need to be the owner of the file or superuser . import core.stdc.errno; import core.sys.posix.pwd; import core.sys.posix.unistd; import std.string; void main(string[] args) { auto username = "gary"; auto file = "test.txt"; auto record = getpwnam(username.toStringz()); if (record !is null) { auto user = record.pw_uid; auto group = record.pw_gid; auto result = chown(file.toStringz(), user, group); if (result != 0) { switch(errno) { case EPERM: // break; default: // break; } } } }
chown
system call works exactly the same as the Unix shell's chown
command. The file name and its new owner and group are indicated. To change the owner of the file, your program must have superuser rights. import core.stdc.errno; import core.sys.posix.unistd; import std.string; void main(string[] args) { auto file = "test.txt"; auto linked = "link.txt"; auto result = link(file.toStringz(), linked.toStringz()); if (result != 0) { switch(errno) { case EPERM: case EACCES: // break; case EEXIST: // break; case ENOENT: // default: // break; } } }
auto result = link(file.toStringz(), linked.toStringz());
auto result = symlink(file.toStringz(), linked.toStringz());
Source: https://habr.com/ru/post/270189/
All Articles