save and load a 2d array to a file

Chris Katko ckatko at gmail.com
Tue Apr 19 05:05:02 UTC 2022


````D
struct map_t{
int data[50][50];
} map;

//save
std.file.write("save.map", map.data); // compiles

//load
map.data = std.file.read("save.map", map.data.sizeof); // error

main.d(536): Error: cannot implicitly convert expression 
`read("save.map", 2500LU)` of type `void[]` to `ubyte[50][]`
````

I'm guessing here, that internally we've got an array of arrays 
(which means array of pointers), and D doesn't know how to split 
by one of the axis. So how do I do that? If I know it's exactly 
packed as it was before, can I smash it somehow by direct writing 
a bunch of ubytes?

What is the 'smash' way to do it, and if better, what's the 
elegant way to do it? (In case I need either going forward).

Thanks,
--Chris


More information about the Digitalmars-d-learn mailing list