View mode: basic / threaded / horizontal-split · Log in · Help
January 23, 2005
BinaryStream? and Hello!
Hello Guys,

I'm moving some c# stuff to D and its been a little rough but not too 
bad. I do not come from a C/C++ background and so some obvious things 
are new to me. Pointers n related are perplexing to me, I'll learn more 
about them later I suppose!

I started a game in C# and the ported it to D. It uses SDL and OpenGL 
(thanks Derelict!) and it works very well. My next step is reading and 
writing game data, my preferred method is to use a binary file (aka DAT 
file). In C# I would do this by using a BinaryStream Reader and Writer.

For example, to read 4 bytes then a long...

byte[] myBytes = new byte[4];
myBytes[0] = reader.ReadByte();
myBytes[1] = reader.ReadByte();
myBytes[2] = reader.ReadByte();
myBytes[3] = reader.ReadByte();

long mySomeWhatBigNumber;

mySomeWhatBigNumber = reader.ReadInt64();


The binary writer was just as easy.

Writer.Write(myBytes[0]); // Automatically write a byte
Writer.Write(mySomeWhatBigNumber); // Automatically write a long

How can I do this in D? Its an absoluate pain to search google, etc for 
such a thing.

Thanks in advance for your time guys!

Nate
January 24, 2005
Re: BinaryStream? and Hello!
Mango's IO package provides stuff like this:

FileConduit fc = new FileConduit("file.txt");
IReader r = new Reader(fc);
byte[] b = new byte[4];
long l;
r.get(b[0]).get(b[1]).get(b[2]).get(b[3]).get(l);

You can get it at dsource.org

John

Nate wrote:
> Hello Guys,
> 
> I'm moving some c# stuff to D and its been a little rough but not too 
> bad. I do not come from a C/C++ background and so some obvious things 
> are new to me. Pointers n related are perplexing to me, I'll learn more 
> about them later I suppose!
> 
> I started a game in C# and the ported it to D. It uses SDL and OpenGL 
> (thanks Derelict!) and it works very well. My next step is reading and 
> writing game data, my preferred method is to use a binary file (aka DAT 
> file). In C# I would do this by using a BinaryStream Reader and Writer.
> 
> For example, to read 4 bytes then a long...
> 
> byte[] myBytes = new byte[4];
> myBytes[0] = reader.ReadByte();
> myBytes[1] = reader.ReadByte();
> myBytes[2] = reader.ReadByte();
> myBytes[3] = reader.ReadByte();
> 
> long mySomeWhatBigNumber;
> 
> mySomeWhatBigNumber = reader.ReadInt64();
> 
> 
> The binary writer was just as easy.
> 
> Writer.Write(myBytes[0]); // Automatically write a byte
> Writer.Write(mySomeWhatBigNumber); // Automatically write a long
> 
> How can I do this in D? Its an absoluate pain to search google, etc for 
> such a thing.
> 
> Thanks in advance for your time guys!
> 
> Nate
January 24, 2005
Re: BinaryStream? and Hello!
Thank you very much John, this is exactly what I needed.

John Demme wrote:
> Mango's IO package provides stuff like this:
> 
> FileConduit fc = new FileConduit("file.txt");
> IReader r = new Reader(fc);
> byte[] b = new byte[4];
> long l;
> r.get(b[0]).get(b[1]).get(b[2]).get(b[3]).get(l);
> 
> You can get it at dsource.org
> 
> John
> 
> Nate wrote:
> 
>> Hello Guys,
>>
>> I'm moving some c# stuff to D and its been a little rough but not too 
>> bad. I do not come from a C/C++ background and so some obvious things 
>> are new to me. Pointers n related are perplexing to me, I'll learn 
>> more about them later I suppose!
>>
>> I started a game in C# and the ported it to D. It uses SDL and OpenGL 
>> (thanks Derelict!) and it works very well. My next step is reading and 
>> writing game data, my preferred method is to use a binary file (aka 
>> DAT file). In C# I would do this by using a BinaryStream Reader and 
>> Writer.
>>
>> For example, to read 4 bytes then a long...
>>
>> byte[] myBytes = new byte[4];
>> myBytes[0] = reader.ReadByte();
>> myBytes[1] = reader.ReadByte();
>> myBytes[2] = reader.ReadByte();
>> myBytes[3] = reader.ReadByte();
>>
>> long mySomeWhatBigNumber;
>>
>> mySomeWhatBigNumber = reader.ReadInt64();
>>
>>
>> The binary writer was just as easy.
>>
>> Writer.Write(myBytes[0]); // Automatically write a byte
>> Writer.Write(mySomeWhatBigNumber); // Automatically write a long
>>
>> How can I do this in D? Its an absoluate pain to search google, etc 
>> for such a thing.
>>
>> Thanks in advance for your time guys!
>>
>> Nate
January 24, 2005
Re: BinaryStream? and Hello!
> For example, to read 4 bytes then a long...
>
> byte[] myBytes = new byte[4];
> myBytes[0] = reader.ReadByte();
> myBytes[1] = reader.ReadByte();
> myBytes[2] = reader.ReadByte();
> myBytes[3] = reader.ReadByte();
> long mySomeWhatBigNumber;
> mySomeWhatBigNumber = reader.ReadInt64();

See phobos' std.stream module: http://www.digitalmars.com/d/std_stream.html
You'll want to use the Stream.read and Stream.write functions.

Phobos is the runtime library that comes with dmd.zip. Comments are welcome 
because phobos is still under development and it's easy to enhance it or fix 
bugs.

-Ben
January 24, 2005
Re: BinaryStream? and Hello!
I should add someone reported a problem with the latest phobos build on 
Windows - it looks like dmd version 0.111 has an out of date stream.obj 
file. Maybe Walter fixed it already. you but just in case you run into 
trouble with 0.111 you should recompile phobos.lib by changing to the 
phobos/srd directory and typing "make -f win32.mak"
January 24, 2005
Re: BinaryStream? and Hello!
Cool! Even better than what I was about to do. Big thanks Ben.

Ben Hinkle wrote:
>>For example, to read 4 bytes then a long...
>>
>>byte[] myBytes = new byte[4];
>>myBytes[0] = reader.ReadByte();
>>myBytes[1] = reader.ReadByte();
>>myBytes[2] = reader.ReadByte();
>>myBytes[3] = reader.ReadByte();
>>long mySomeWhatBigNumber;
>>mySomeWhatBigNumber = reader.ReadInt64();
> 
> 
> See phobos' std.stream module: http://www.digitalmars.com/d/std_stream.html
> You'll want to use the Stream.read and Stream.write functions.
> 
> Phobos is the runtime library that comes with dmd.zip. Comments are welcome 
> because phobos is still under development and it's easy to enhance it or fix 
> bugs.
> 
> -Ben 
> 
>
Top | Discussion index | About this forum | D home