www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Binary IO

reply "seany" <seany uni-bonn.de> writes:
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.
Jul 17 2014
next sibling parent Justin Whear <justin economicmodeling.com> writes:
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.
Jul 17 2014
prev sibling parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
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
Jul 17 2014
parent reply "seany" <seany uni-bonn.de> writes:
Data is a built in type? what includefile do I need?
Jul 17 2014
next sibling parent Justin Whear <justin economicmodeling.com> writes:
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?
Jul 17 2014
prev sibling parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
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
Jul 17 2014
parent "Alexandre" <alebencz gmail.com> writes:
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
Jul 18 2014