简体   繁体   中英

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

I read somewhere that the std.stdio's File loads the file in the memory, then reads it. I come from Delphi, where there was a TFileStream class, which could be used to read/write files, without loading them in memory. And when writing/reading, I didn't have to worry about the line endings or any special characters, I could provide the number of bytes I want to read/write, plus it could even read/write to/from array of bytes, I'm not sure std.stdio.File has it or not.

Is it possible in D as well? I want to read only a number of bytes from a file, from a position I provide, without loading the whole file into the memory (the file is very big)? If yes, then how? And one more thing, I'll have to read files to array of ubytes(0-255)

PS: the method should work on Linux, Windows is not my target;

What you want is File and using the methods seek and rawRead/rawWrite. It just wraps around the c library, nothing too fancy here.

No module in the D standard library does what you say you've " read somewhere ". However, there is a function in the std.file module which can be used to read entire file - the read() function . This function has signature void[] read(R)(R name, size_t upTo = size_t.max) and can be used to read upTo number of bytes. Naturally, if you omit the upTo parameter it will read the whole file (or fill up your memory!).

As suggested by Richard, use the File wrapper from the std.stdio module to do arbitrary seek() and read() operation.

If your file is large, it is maybe a good idea to use the 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM