Write struct to file
Andrej Mitrovic
andrej.mitrovich at gmail.com
Sat Feb 25 14:39:36 PST 2012
On 2/25/12, Ali Çehreli <acehreli at yahoo.com> wrote:
> 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.
I'm not sure where you're getting that from:
import std.stdio;
struct nagger
{
string name;
int age;
double weight;
string msg;
}
void main(string[] args)
{
if (args[1] == "write")
{
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());
}
else
if (args[1] == "read")
{
nagger dup;
auto file = File("test.bin", "r");
auto readBytes = fread(&dup, byte.sizeof, dup.sizeof, file.getFP());
writeln(dup);
}
}
$ rdmd test.d write
$ rdmd test.d read
nagger("name", 23, 108.5, "msg")
More information about the Digitalmars-d-learn
mailing list