Path.readFile

Read entire contents of file name and returns it as an untyped array. If the file size is larger than upTo, only upTo bytes are _read.

struct Path
const
readFile
(
size_t upTo = size_t.max
)

Parameters

upTo size_t

if present, the maximum number of bytes to _read

Return Value

Type: auto

Untyped array of bytes _read

Throws

FileException in case of error

Examples

import dshould;
Path root = createTempPath();
scope(exit) root.remove();

root.join("test-create.txt").exists.should.be(false);

// Test file read/write/apppend
root.join("test-create.txt").writeFile("Hello World");
root.join("test-create.txt").exists.should.be(true);
root.join("test-create.txt").readFile.should.equal("Hello World");
root.join("test-create.txt").appendFile("!");
root.join("test-create.txt").readFile.should.equal("Hello World!");

// Try to remove file
root.join("test-create.txt").exists.should.be(true);
root.join("test-create.txt").remove();
root.join("test-create.txt").exists.should.be(false);

// Try to read data as bytes
ubyte[] data = [1, 7, 13, 5, 9];
root.join("test-write-2.txt").exists.should.be(false);
root.join("test-write-2.txt").writeFile(data);
root.join("test-write-2.txt").exists.should.be(true);
ubyte[] rdata = cast(ubyte[])root.join("test-write-2.txt").readFile;
rdata.length.should.equal(5);
rdata[0].should.equal(1);
rdata[1].should.equal(7);
rdata[2].should.equal(13);
rdata[3].should.equal(5);
rdata[4].should.equal(9);

Meta