Thread overview
Binary IO
Jul 17, 2014
seany
Jul 17, 2014
Justin Whear
Jul 17, 2014
H. S. Teoh
Jul 17, 2014
seany
Jul 17, 2014
Justin Whear
Jul 17, 2014
H. S. Teoh
Jul 18, 2014
Alexandre
July 17, 2014
Hello,

What are the methods of unformatted binary IO in d? File.write seems to use formatted ASCII . I would like to write a binary file that I cna read in fortan. Similarly, I would like to write a file in Fortan, unformatted IO, and read it using D.
July 17, 2014
On Thu, 17 Jul 2014 20:35:24 +0000, seany wrote:

> Hello,
> 
> What are the methods of unformatted binary IO in d? File.write seems to use formatted ASCII . I would like to write a binary file that I cna read in fortan. Similarly, I would like to write a file in Fortan, unformatted IO, and read it using D.

You have a few options:
 * The old std.stream -- this module is due for replacement, hopefully
ASAP.
 * Use File.rawRead/rawWrite.  These are intended for arrays, though they
can be used to read single values.
 * Work with chunks of ubyte data and use std.bitmanip's read and write
functions.

The last option is probably your best option for producing good future- proof, idiomatic D code.
July 17, 2014
On Thu, Jul 17, 2014 at 08:35:24PM +0000, seany via Digitalmars-d-learn wrote:
> Hello,
> 
> What are the methods of unformatted binary IO in d? File.write seems to use formatted ASCII . I would like to write a binary file that I cna read in fortan. Similarly, I would like to write a file in Fortan, unformatted IO, and read it using D.

Use File.rawWrite:

	auto f = File("myfile", "w");
	Data[] data = ... /* put data here */;
	f.rawWrite(data);

Similarly, to read binary data, use File.rawRead:

	auto f = File("myfile", "r");
	Data[] buf; /* buffer to store the data */
	buf.length = /* number of data items to read */;
	auto data = f.rawRead(buf);
	/* data will be a slice of buf, with .length containing the
	 * actual number of items read */

You can use ubyte[] if you have byte-based data to read/write, but rawRead / rawWrite are flexible enough to take arrays of any type.


T

-- 
You are only young once, but you can stay immature indefinitely. -- azephrahel
July 17, 2014
Data is a built in type? what includefile do I need?
July 17, 2014
On Thu, 17 Jul 2014 21:01:35 +0000, seany wrote:

> Data is a built in type? what includefile do I need?

No, just used as an example.  What sort of data are reading from the binary file?
July 17, 2014
On Thu, Jul 17, 2014 at 09:01:35PM +0000, seany via Digitalmars-d-learn wrote:
> Data is a built in type? what includefile do I need?

It can be any type you want, it was just an example.


T

-- 
Chance favours the prepared mind. -- Louis Pasteur
July 18, 2014
Maybe this can be usefull for u

https://gist.github.com/bencz/3576dfc8a217a34c05a9

On Thursday, 17 July 2014 at 23:04:06 UTC, H. S. Teoh via Digitalmars-d-learn wrote:
> On Thu, Jul 17, 2014 at 09:01:35PM +0000, seany via Digitalmars-d-learn wrote:
>> Data is a built in type? what includefile do I need?
>
> It can be any type you want, it was just an example.
>
>
> T