Writing and Reading Binary Data Files

663 views
Skip to first unread message

MiniMonster

unread,
Jun 21, 2015, 11:31:13 AM6/21/15
to swift-l...@googlegroups.com
I'm having trouble figuring out how to write and read binary data files using swift.
Using OS X  Command Line Tool application in Xcode 6
I'm looking for help / info / resource for understanding how to create, open, write to, and read Doubles from a file in my documents directory.   Eventually i'll need to write and read an array of Doubles.  
I've been all over the web and there doesn't seem to be a clear explanation.  Basic texts don't even touch the subject.  Any help would be appreciated.  Tnx

Kevin Greene

unread,
Jun 22, 2015, 2:37:21 AM6/22/15
to MiniMonster, swift-l...@googlegroups.com
This question is pretty vague. What format do you want to store the Doubles in? You could convert them to UTF8 strings and store them as comma separated values. You have many different options.
--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swift-languag...@googlegroups.com.
To post to this group, send email to swift-l...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/cbf943f9-99b5-4d7e-912d-62c9fd2530c7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jens Alfke

unread,
Jun 22, 2015, 12:07:42 PM6/22/15
to Kevin Greene, MiniMonster, swift-l...@googlegroups.com

On Jun 21, 2015, at 11:37 PM, Kevin Greene <kgre...@gmail.com> wrote:

This question is pretty vague. What format do you want to store the Doubles in?

They did say “binary”. To me that implies the file is a simple sequence of (big-endian?) 64-bit IEEE doubles.

The rough outline is that you’d do the conversion as
[Double] <-> UnsafeMutablePointer<Double> <-> NSData <-> file

—Jens

Jens Alfke

unread,
Jun 22, 2015, 2:14:03 PM6/22/15
to Kevin Greene, MiniMonster, swift-l...@googlegroups.com

On Jun 22, 2015, at 9:07 AM, Jens Alfke <je...@mooseyard.com> wrote:

The rough outline is that you’d do the conversion as
[Double] <-> UnsafeMutablePointer<Double> <-> NSData <-> file

Oops, should add that there’s a bit more work if you want to be endian-safe. Unfortunately I don’t know how to use byte-swapping functions from Swift — ordinarily I’d say to use NSSwapHostDoubleToBig and NSSwapBigDoubleToHost, but I don’t know if those are callable from Swift since they’re defined as inline in C.

—Jens

MiniMonster

unread,
Jun 23, 2015, 6:21:28 AM6/23/15
to swift-l...@googlegroups.com
Kevin and Jens, thanks for weighing in on this.  I came from a C#.net environment where the stream model was pretty straightforward:
1.  Create a file stream object  
2.  Open the stream for either input or output using a path name
3.  Write formatted data (Ints, Strings, or floats) to the stream OR Read formatted data from the stream
4.  Close the stream when done.

Say I need to create a file of 100 Double values, store it to a data.dat file, and read it back into an array later for processing.  I guess what I'm asking is how to use the foundation framework and swift to create, open, write, read, close, and otherwise manage a stream to/from a file on my hard drive.

Thanks for your help.

- John

Jens Alfke

unread,
Jun 23, 2015, 12:09:16 PM6/23/15
to MiniMonster, swift-l...@googlegroups.com

> On Jun 23, 2015, at 3:21 AM, MiniMonster <john.m...@md.metrocast.net> wrote:
>
> Say I need to create a file of 100 Double values, store it to a data.dat file, and read it back into an array later for processing. I guess what I'm asking is how to use the foundation framework and swift to create, open, write, read, close, and otherwise manage a stream to/from a file on my hard drive.

{The Swift standard library doesn’t cover I/O (besides println) so this is verging on off-topic, but on the other hand I/O is one of those low-level things that you tend to expect a language’s standard library to cover, so it seems reasonable to talk about it here…}

Streams are given a lot less priority in Foundation than in most other platforms. They exist but they’re not generally used for simple I/O. Instead there are individual method calls that read and write an entire file. I generally like this because it’s less code to write, and it’s probably also somewhat faster. It also gives you some convenient options like an atomic replace (“safe save”) or memory-mapping the data.

The lowest-level file I/O methods are
NSData(contentsOfURL: …, options: …, error: …)
NSData.writeToURL(options: …, error: …)

So what you’d do is get an (unsafe) pointer to your Array, create an NSData from it, then call writeToURL. In the other direction you’d create an NSData with the contents of the file, then use the ‘bytes’ pointer to initialize an Array<Double>.

—Jens

MiniMonster

unread,
Jun 23, 2015, 5:26:19 PM6/23/15
to swift-l...@googlegroups.com
Thanks Jens for the insights.  I'll expariment some and get this to work for me.  -John 


On Sunday, June 21, 2015 at 11:31:13 AM UTC-4, MiniMonster wrote:
Reply all
Reply to author
Forward
0 new messages