January 21, 2007
Heinz wrote:
> In C++ you can write an entire structure to a binary file:
> 
> http://www.gamedev.net/reference/articles/article1127.asp
> http://www.codersource.net/cpp_file_io_binary.html
> 
> Can you do the same in D?

Sure, and it will work between instances of the program so long as none of the structure's members are referances: pointers, object variables, arrays.

-- Chris Nicholson-Sauls
January 21, 2007
Chris Nicholson-Sauls Wrote:

> Heinz wrote:
> > In C++ you can write an entire structure to a binary file:
> > 
> > http://www.gamedev.net/reference/articles/article1127.asp http://www.codersource.net/cpp_file_io_binary.html
> > 
> > Can you do the same in D?
> 
> Sure, and it will work between instances of the program so long as none of the structure's members are referances: pointers, object variables, arrays.
> 
> -- Chris Nicholson-Sauls

So, you mean i can't have this structure because i has an array?

struct h
{

}

Could you post an example please?
January 21, 2007
Chris Nicholson-Sauls Wrote:

> Heinz wrote:
> > In C++ you can write an entire structure to a binary file:
> > 
> > http://www.gamedev.net/reference/articles/article1127.asp http://www.codersource.net/cpp_file_io_binary.html
> > 
> > Can you do the same in D?
> 
> Sure, and it will work between instances of the program so long as none of the structure's members are referances: pointers, object variables, arrays.
> 
> -- Chris Nicholson-Sauls

What about classes can they be written under the same rules?
January 21, 2007
Heinz wrote:
> Chris Nicholson-Sauls Wrote:
> 
>> Heinz wrote:
>>> In C++ you can write an entire structure to a binary file:
>>>
>>> http://www.gamedev.net/reference/articles/article1127.asp
>>> http://www.codersource.net/cpp_file_io_binary.html
>>>
>>> Can you do the same in D?
>> Sure, and it will work between instances of the program so long as none of the structure's members are referances: pointers, object variables, arrays.
>>
>> -- Chris Nicholson-Sauls
> 
> So, you mean i can't have this structure because i has an array?
> 
> struct h
> {
> 
> }
> 
> Could you post an example please?

Right, because these arrays are essentially a pointer to data somewhere else, they don't exist in the same block of memory.

To do it automatically you would need some form of metadata (which would identify pointers) or something like serialization (which handled each element on its own).

In D and C++ you can read a block like below in one go:

struct h
{
  int x;
  int y;
  char a;
  char b[100];   //Note because this is constant its included in this block.
};

However you can't write something like this in D or C++:

struct h
{
  int x;
  int y;
  char a;
  char* b;  //This is pointing elsewhere in memory.  You'll need to fix this pointer up when you read it in.
};

Since D dynamic arrays are really:

struct Darray
{
   size_t length;
   T* type;      //Pointer to some location
};

You can't save these out inside a struct.  You need to save the data it points to as well.

-Joel
January 21, 2007
You can take a look at the source of a serialisation library. E.g. see
this thread:
"serialization library" in the group D.announce on 8th Nov 2006



January 21, 2007
"Heinz" <billgates@microsoft.com> wrote in message news:eoualo$1rcv$1@digitaldaemon.com...
>
> Wow, that covers all, thanks for your reply.
>
> But, can i still write an entire structure with writeExact()? or you suggest writting each member of the structure with write()?

Yeah, that's perfectly fine as long as the structure doesn't contain any reference members (pointers, class references, dynamic arrays).  Binary files a lot of times have some kind of standard header which can be written or read in one big chunk, which is possible to do with a structure.

But if the structure contains any reference members, writing it out with writeExact will not work, and you'll have to write out the members manually.


January 21, 2007
"Heinz" <billgates@microsoft.com> wrote in message news:eoug19$22to$1@digitaldaemon.com...
> Chris Nicholson-Sauls Wrote:
>
> What about classes can they be written under the same rules?

Class instances == object variables.  All instances of classes are references (pointers) implicitly.


January 25, 2007
On Sun, 21 Jan 2007 03:15:38 +0100, Frank Benoit (keinfarbton) <benoit@tionex.removethispart.de> wrote:

> You can take a look at the source of a serialisation library. E.g. see
> this thread:
> "serialization library" in the group D.announce on 8th Nov 2006

Up to date versions of that library are found at
http://www.dsource.org/projects/serialization

Christian
1 2
Next ›   Last »