Binary IO

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 17 13:52:41 PDT 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


More information about the Digitalmars-d-learn mailing list