On 02/25/2012 10:33 AM, Andrej Mitrovic wrote:
Well first I'd recommend not allocating the struct on the heap. Then you can do:
import std.stdio;
struct nagger
{
string name;
int age;
double weight;
string msg;
}
void main()
{
nagger lal;
lal.name = "name";
lal.age = 23;
lal.weight = 108.5;
lal.msg = "msg";
auto file = File("test.bin", "w");
auto writeBytes = fwrite(&lal, byte.sizeof, lal.sizeof, file.getFP());
But there is no way for fwrite to follow name.ptr to also write the
characters that are in the string, right?
file.close();
nagger dup;
file = File("test.bin", "r");
auto readBytes = fread(&dup, byte.sizeof, dup.sizeof, file.getFP());
assert(lal == dup);
That passes because lal.name.ptr and dup.name.ptr have the same value.
Maybe that wasn't the intention but the data is not really in the file.
}
This doesn't work for heap-allocated structs.
Ali