Writing and Reading Binary Data Files

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Writing and Reading Binary Data Files

MiniMonster
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: Writing and Reading Binary Data Files

Kevin Greene
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.

On Sunday, June 21, 2015, MiniMonster <[hidden email]> wrote:
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

--
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 <a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;swift-language%2Bunsubscribe@googlegroups.com&#39;);" target="_blank">swift-language+unsubscribe@....
To post to this group, send email to <a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;swift-language@googlegroups.com&#39;);" target="_blank">swift-language@....
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/CAEY8wq62TF1RjriETt4s3Baav_Pw7_8xZ6Hx0OC93zXBEpuo3A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Writing and Reading Binary Data Files

Jens Alfke

On Jun 21, 2015, at 11:37 PM, Kevin Greene <[hidden email]> 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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/C3FE8092-172C-4130-9D5C-22CEEB1A1F3A%40mooseyard.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Writing and Reading Binary Data Files

Jens Alfke

On Jun 22, 2015, at 9:07 AM, Jens Alfke <[hidden email]> 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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/5E8E4E08-B25B-4ADD-A3B9-E5BDB6AA0002%40mooseyard.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Writing and Reading Binary Data Files

MiniMonster
In reply to this post by MiniMonster
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

On Sunday, June 21, 2015 at 11:31:13 AM UTC-4, MiniMonster wrote:
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/9f2b0c27-e20c-499b-9ec3-26c074d9e5cb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Writing and Reading Binary Data Files

Jens Alfke

> On Jun 23, 2015, at 3:21 AM, MiniMonster <[hidden email]> 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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/44DB30E6-6924-4913-94DE-2AAAAEB9EA81%40mooseyard.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Writing and Reading Binary Data Files

MiniMonster
In reply to this post by MiniMonster
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:
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/26c9cda6-468c-41a1-b4c5-3c9af1aa3c49%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.