Re: Write struct to file

2012-02-27 Thread Chopin
Ah, I get it now. Thank you so much! I think I will go for JSON. It will be mostly string data anyway :)!

Re: Write struct to file

2012-02-26 Thread Chopin
Hello! OP here :) I'm gonna be more precise why I want this. I'm writing my own blog, I will write my own HTTP server etc. and the saving of the data myself. Everything will be done in D! Why? Learn D. I don't want to hear the reinvent the wheel :(, I think you learn a lot by doing thing like

Re: Write struct to file

2012-02-26 Thread Ali Çehreli
On 02/26/2012 03:22 PM, Chopin wrote: I don't want to hear the reinvent the wheel :(, I think you learn a lot by doing thing like this. Agreed. So, when I POST a blog-entry, I will read it, and write it to a file. I don't want write strings in a file, like: 1--||--Title--||--The

Re: Write struct to file

2012-02-26 Thread Andrej Mitrovic
On 2/27/12, Ali Çehreli acehr...@yahoo.com wrote: D is awesome compared to C as it enables serializing/deserializing data with its generic programming and compile-time reflection features like this: http://dlang.org/traits.html#allMembers ae's json uses .tupleof, and does it in 40(!)*

Re: Write struct to file

2012-02-25 Thread Timon Gehr
On 02/25/2012 07:03 PM, Chopin wrote: Hello! import std.stdio; struct nagger { string name; int age; double weight; string msg; } void main() { auto lal = new nagger(); lal.name = AHAHAHAHHA; lal.age = 23; lal.weight = 108.5; lal.msg = fgfdgfdgfdgfdgfdgfdg; writeln(cast(ubyte[])(lal)); }

Re: Write struct to file

2012-02-25 Thread Timon Gehr
On 02/25/2012 07:15 PM, Timon Gehr wrote: On 02/25/2012 07:03 PM, Chopin wrote: Hello! import std.stdio; struct nagger { string name; int age; double weight; string msg; } void main() { auto lal = new nagger(); lal.name = AHAHAHAHHA; lal.age = 23; lal.weight = 108.5; lal.msg =

Re: Write struct to file

2012-02-25 Thread Andrej Mitrovic
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

Re: Write struct to file

2012-02-25 Thread Andrej Mitrovic
On 2/25/12, Andrej Mitrovic andrej.mitrov...@gmail.com wrote: This doesn't work for heap-allocated structs. Sorry my bad. .sizeof should always be set on Types and not variable names, because a struct pointer will have sizeof == size_t, whereas a simple struct variable will have sizeof equal to

Re: Write struct to file

2012-02-25 Thread Ali Çehreli
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;

Re: Write struct to file

2012-02-25 Thread Andrej Mitrovic
On 2/25/12, Ali Çehreli acehr...@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;

Re: Write struct to file

2012-02-25 Thread Andrej Mitrovic
To be honest the C fread and fwrite aren't even necessary since you can do a rawRead and rawWrite instead.

Re: Write struct to file

2012-02-25 Thread Andrej Mitrovic
On 2/25/12, Ali Çehreli acehr...@yahoo.com wrote: But there is no way for fwrite to follow name.ptr to also write the characters that are in the string, right? Oh my I just got a big fat zero on the finals. You're absolutely right, what gets copied is the length and the pointer. The only reason

Re: Write struct to file

2012-02-25 Thread Andrej Mitrovic
On 2/26/12, Andrej Mitrovic andrej.mitrov...@gmail.com wrote: allocated on the stack Sorry, I meant the data segment not the stack. That's -1 score for me.

Re: Write struct to file

2012-02-25 Thread Andrej Mitrovic
On 2/25/12, Andrej Mitrovic andrej.mitrov...@gmail.com wrote: I'm not sure where you're getting that from: Let that be a classic lesson on what never to do. Here's a demonstration on how wrong I was: import std.stdio; struct Foo { char[] name; } void main(string[] args) { if (args[1]

Re: Write struct to file

2012-02-25 Thread Ali Çehreli
On 02/25/2012 03:00 PM, Andrej Mitrovic wrote: On 2/25/12, Ali Çehreliacehr...@yahoo.com wrote: But there is no way for fwrite to follow name.ptr to also write the characters that are in the string, right? Oh my I just got a big fat zero on the finals. You're absolutely right, what gets