On Monday, 16 March 2020 at 13:09:08 UTC, Adnan wrote:
On Sunday, 15 March 2020 at 00:37:35 UTC, H. S. Teoh wrote:
On Sat, Mar 14, 2020 at 10:37:37PM +0000, Adnan via Digitalmars-d-learn wrote:
[...]

That's because a memory-mapped file appears directly in your program's memory address space as if it was an array of bytes (ubyte[]). No interpretation is imposed upon the contents. If you want lines out of it, try casting the memory to const(char)[] and using std.algorithm.splitter to get a range of lines. For example:

        auto mmfile = new MmFile("myfile.txt");
        auto data = cast(const(char)[]) mmfile[];
        auto lines = data.splitter("\n");
        foreach (line; lines) {
                ...
        }


T

Would it be wasteful to cast the entire content into a const string? Can a memory mapped file be read with a buffer?

a string is the same thing as immutable(char)[] . It would make no difference with the example above.

Reply via email to