如何读取文件而不将其加载到内存中,例如 D 中的 Delphi TFileStream class?

How do I read a file, wihout loading it into the memory, like the Delphi TFileStream class in D?

我在某处读到 std.stdio 的文件将文件加载到内存中,然后读取它。我来自 Delphi,那里有一个 TFileStream class,它可以用于 read/write 文件,而无需将它们加载到内存中。当 writing/reading 时,我不必担心行尾或任何特殊字符,我可以提供我想要 read/write 的字节数,而且它甚至可以 read/write to/from字节数组,我不确定std.stdio。文件有没有。

D也可以吗?我只想从文件中读取一些字节,从我提供的位置,而不是将整个文件加载到内存中(文件很大)?如果是,那么如何?还有一件事,我必须将文件读取到 ubytes(0-255)

数组

P.S:该方法应该适用于Linux,Windows不是我的目标;

你想要的是 File 并使用方法 seek 和 rawRead/rawWrite。 它只是围绕着 c 库,这里没什么特别的。

D 标准库中的任何模块都不会执行您所说的“在某处阅读”。但是,std.file模块中有一个函数可以用来读取整个文件——read() function。此函数具有签名 void[] read(R)(R name, size_t upTo = size_t.max),可用于读取 upTo 个字节。当然,如果您省略 upTo 参数,它将读取整个文件(或填满您的内存!)。

按照 Richard 的建议,使用 std.stdio 模块中的 File 包装器来执行任意的 seek() 和 read() 操作。

如果您的文件很大,使用 std.mmfile. It is very convenient and allows you to deal with the memory mapped file like it is a slice (array)... Related SO thread

可能是个好主意