writing uint16's to a binary file

Ken G. Brown kbrown at tnc.ab.ca
Mon Mar 11 18:53:33 UTC 2002


Thx for the quick responses. See inline comments...

> -----Original Message-----
> From: squeak-dev-admin at lists.squeakfoundation.org
> [mailto:squeak-dev-admin at lists.squeakfoundation.org]On Behalf Of Bijan
> Parsia
> Sent: March 11, 2002 11:23 AM
> To: squeak-dev at lists.squeakfoundation.org
> Subject: Re: writing uint16's to a binary file
>
>
> On Mon, 11 Mar 2002, Ken G. Brown wrote:
>
> > I'm needing to write a bunch of lines of uint16's to a
> binary file. So far I
> > have a variation of the following but it gives a message
> not understood,
> > adaptToNumber:andSend:.
>
> Er..I found this to be rather confusing code (and I don't
> have uin16 in my
> image). Some comments.
>
> > test _ #(1 2 3 4 5 6 7 8).
> > rStr _ ReadStream on: test.
>
> WHy the ReadStream?

Confusing code because I'm confused. I admit it! :)
ReadStream appeared to have the uint16 possibility from PositionableStream.
I had tried going directly on the FileStream from collection but iut seemed
to want to output 32 bits for each element and I have to have 16 bits.
Basically I'm generating simulated values for a 16 bit analog inputs.

> > outfile _ fileStream _ (FileStream newFileNamed: 'outfile') binary.
>
> Why the double assignment?

Doh! Cut and paste error. And I really tried to get it right too!

> > outfile nextPutAll: (rStr do: [ :ii | rStr uint16]).
>
> This is definitely broken in a couple of places, most notable in the
> block. Surely you want ii uint16? Second, if uint16 *returns*
> something,
> rather than modifying the receiver (which I suspect will be
> the case) then
> you won't have "done" anything that outfile will see. (I'm a bit
> suspicious about nextPutAll:ing a stream anyway).

My intent was to retrieve the next element of the collection as a uint16,
via the ReadStream on the collection. I'll be trying out these suggested
variations.
  Thx
  Ken

> Perhaps
> 	outfile nextPutAll: (test collect: [:ii | ii uint16]).
>
> (Collect returns the collection of the results of the block
> as applied to
> the receiver's elements.) So the more intelligible to me,
> although perhaps
> incorrect, version is:
>
> 	test _ #(1 2 3 4 5 6 7 8).
> 	outfile  _ (FileStream newFileNamed: 'outfile') binary.
> 	outfile nextPutAll: (test collect: [ :ii | rStr uint16]).
> 	outfile close.
>
> However, you may consider using some of the "nonhomogeneous
> access" methods from PositionableStream, e.g., #nextInt32Put:,
> #nextNumber:put: and friends. In that case, you may put that
> inside the
> loop:
>
> 	test _ #(1 2 3 4 5 6 7 8).
>         outfile  _ (FileStream newFileNamed: 'outfile') binary.
> 	test do: [:ii | outfile nextIn32Put: ii].
> 	outfile close.
>
> Finally, it's often a good idea to use an #ensure: to make
> sure your file
> closes (in a timely fashion...finalization will catch it
> eventuallly) even
> if you get an error. Something like:
>
> 	[test do: [:ii | outfile nextInt32Put: ii]]
> 		ensure: [outfile close].
>
> Cheers,
> Bijan Parsia.
>




More information about the Squeak-dev mailing list