digitalmars.D.learn - Consume binary files
- Bogdan (18/18) Mar 10 2018 I'm working on a pet project which involves reading various
- Bogdan (6/6) Mar 10 2018 ... I accidentally posted that before it was complete because I
- Jonathan M Davis (11/17) Mar 10 2018 Check out
- Bogdan (4/15) Mar 10 2018 Yes, thank you! That's much better, even if the source buffer
- Jonathan M Davis (4/20) Mar 10 2018 read consumes and peek just looks, though you need random access to peek
- Mark Fisher (2/5) Mar 10 2018 I think you should use ranged types.
I'm working on a pet project which involves reading various structure types, or just multi-byte values (uin32_t, uint16_t, etc) from files, or just from ubyte arrays. Here's how I've been dealing with some of these situations so far: ``` /// Helper structure used to read each of the file descriptors in a HOG file struct HogFileDescriptor { align(1): char[13] fileName; int fileSize; } ubyte[HogFileDescriptor.sizeof] buffer; File f = File(filename, "r"); // while bytes left in file f.rawRead(buffer); hfd = cast(HogFileDescriptor*) buffer.ptr;
Mar 10 2018
... I accidentally posted that before it was complete because I kept pressing TAB in order to indent ... Anyway, I'd like to know if there exists such a thing as ``` int a = stream.ReadInt32(); ```
Mar 10 2018
On Saturday, March 10, 2018 18:31:23 Bogdan via Digitalmars-d-learn wrote:... I accidentally posted that before it was complete because I kept pressing TAB in order to indent ... Anyway, I'd like to know if there exists such a thing as ``` int a = stream.ReadInt32(); ```Check out https://dlang.org/phobos/std_bitmanip.html#peek https://dlang.org/phobos/std_bitmanip.html#read They can be used to read integral values from a range of ubytes. You can use either std.file.read or std.stdio.File to read from the file and than those functions from std.bitmanip to convert the ubytes to integrals. std.file.read would be the easiest to use, since it just gives you a single dynamic array to deal with, but it does mean reading in the entire file at once. - Jonathan M Davis
Mar 10 2018
On Saturday, 10 March 2018 at 18:49:48 UTC, Jonathan M Davis wrote:Check out https://dlang.org/phobos/std_bitmanip.html#peek https://dlang.org/phobos/std_bitmanip.html#read They can be used to read integral values from a range of ubytes. You can use either std.file.read or std.stdio.File to read from the file and than those functions from std.bitmanip to convert the ubytes to integrals. std.file.read would be the easiest to use, since it just gives you a single dynamic array to deal with, but it does mean reading in the entire file at once. - Jonathan M DavisYes, thank you! That's much better, even if the source buffer gets consumed.
Mar 10 2018
On Saturday, March 10, 2018 19:22:43 Bogdan via Digitalmars-d-learn wrote:On Saturday, 10 March 2018 at 18:49:48 UTC, Jonathan M Davis wrote:read consumes and peek just looks, though you need random access to peek beyond the front of the range. - Jonathan M DavisCheck out https://dlang.org/phobos/std_bitmanip.html#peek https://dlang.org/phobos/std_bitmanip.html#read They can be used to read integral values from a range of ubytes. You can use either std.file.read or std.stdio.File to read from the file and than those functions from std.bitmanip to convert the ubytes to integrals. std.file.read would be the easiest to use, since it just gives you a single dynamic array to deal with, but it does mean reading in the entire file at once.Yes, thank you! That's much better, even if the source buffer gets consumed.
Mar 10 2018
On Saturday, 10 March 2018 at 18:26:43 UTC, Bogdan wrote:I'm working on a pet project which involves reading various structure types, or just multi-byte values (uin32_t, uint16_t, etc) from files, or just from ubyte arrays.I think you should use ranged types.
Mar 10 2018