binary file read/write acces

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

binary file read/write acces

Ernest Micklei-2
I am looking for the correct code
to open a file for binary reading and
writing. According to the comments,
the following should work. It doesn't.

| w |
w := (File open: 'bin.dat' mode: #open check: false) readWriteStream
beBinary.
[w nextPut: 255.
w reset.
w next inspect]
    ensure:
    [w close]

Instead, I get an end of stream error.
Is it because the stream is for write once read many ?

Ernest Micklei
PhilemonWorks.com


Reply | Threaded
Open this post in threaded view
|

Re: binary file read/write acces

Ian Bartholomew-4
Ernest,

> I am looking for the correct code
> to open a file for binary reading and
> writing. According to the comments,
> the following should work. It doesn't.

(Slight detour) You can make it a bit tidier by replacing the first line
with

w := (FileStream readWrite: 'bin.dat') beBinary.

which, IMHO, is a bit easier to read and does the same thing. It doesn't
help with your error though!. You can get round that by ensuring the file is
flushed before it is reset

w
    flush;
    reset.

Whether this is a bug or just a side effect of the way Dolphin handles
ReadWriteStream (and subclasses) I can't decide. I think it's the latter and
that you just have to be a bit careful when changing the mode of a stream
like this so make sure both the read and write pointers are updated.

Ian