Writing to File Streams 
Author Message
 Writing to File Streams

Thank you to all of the individuals that helped me with the recursion
problem I had previsously. Whilst the help is greatly appreciated, it does
tend to make procedural programmes appear a little more "user friendly" I
fear. Then again its probably a case of what you are used to.

I have been wandering away from the outlined excersizes that I have in my
Smalltalk text book, partly because some are very easy and others presume
that you already have a Masters in programming.

I drafted a method for adding text into a file stream directly. Rather than
taking the data from the file stream, placing it in an array, then amending
it and placing it back in it's file stream. The method ( to my own
surprise ), actually worked, but it places the new text over the first line
of the file as it was before, not much use. Although knowing the answer may
prove quite useless, I am curious. How do I control the placing /
positioning of text into a file stream, without having an array in the
middle of the process ?

Class method for adding data to a file. Class is "Food",  ( not that it
matters ).

addTo: aFile for: aString
    | outputStream |
    outputStream := File pathName: aFile.
    outputStream nextPutAll: aString ; cr.
    outputStream close.

I live in hope that I start to understand this language, but right now I
wish the information could be uploaded to my brain like in "The Matrix".
Thanks in anticipation.

Kelly Tait.



Wed, 18 Jun 1902 08:00:00 GMT  
 Writing to File Streams
Quote:

> Thank you to all of the individuals that helped me with the recursion
> problem I had previsously. Whilst the help is greatly appreciated, it does
> tend to make procedural programmes appear a little more "user friendly" I
> fear. Then again its probably a case of what you are used to.

> I have been wandering away from the outlined excersizes that I have in my
> Smalltalk text book, partly because some are very easy and others presume
> that you already have a Masters in programming.

> I drafted a method for adding text into a file stream directly. Rather than
> taking the data from the file stream, placing it in an array, then amending
> it and placing it back in it's file stream. The method ( to my own
> surprise ), actually worked, but it places the new text over the first line
> of the file as it was before, not much use. Although knowing the answer may
> prove quite useless, I am curious. How do I control the placing /
> positioning of text into a file stream, without having an array in the
> middle of the process ?

Look at the methods #position: and #skip: implemented in
PositionableStream (this protocol is shared by Internal and
ExternalStreams alike).

- Show quoted text -

Quote:

> Class method for adding data to a file. Class is "Food",  ( not that it
> matters ).

> addTo: aFile for: aString
>     | outputStream |
>     outputStream := File pathName: aFile.
>     outputStream nextPutAll: aString ; cr.
>     outputStream close.

> I live in hope that I start to understand this language, but right now I
> wish the information could be uploaded to my brain like in "The Matrix".
> Thanks in anticipation.

> Kelly Tait.

HTH

Nick



Wed, 18 Jun 1902 08:00:00 GMT  
 Writing to File Streams

: Whilst the help is greatly appreciated, it does tend to make
: procedural programmes appear a little more "user friendly" I
: fear. Then again its probably a case of what you are used to.

Also, "procedural" code is simpler for simple things. As your
application becomes more complicated, OOP helps to manage the
complexity better.

: How do I control the placing / positioning of text into a file
: stream, without having an array in the middle of the process ?

Look at PositionableStream if you are using VisualWorks, probably the
same class in other Smalltalks, somewhere in the Stream
hierarchy. Look for #position and #position: methods.

: Class method for adding data to a file. Class is "Food",  ( not that it
: matters ).

: addTo: aFile for: aString
:     | outputStream |
:     outputStream := File pathName: aFile.
:     outputStream nextPutAll: aString ; cr.
:     outputStream close.

You don't have to put "scratch" code in a class just to execute it. As
you say there is no using Food in this method. Instead you can put the
code in a "workspace" window and execute it from "do it" or "print it"
menu picks.

: I live in hope that I start to understand this language, but right
: now I wish the information could be uploaded to my brain like in
: "The Matrix".

Why not post the name of the city where you live, and ask in this
group if someone nearby is willing to spend a lunch hour giving you an
introduction to Smalltalk and its tools? The best way to learn is from
someone who knows and can show you.

--



Wed, 18 Jun 1902 08:00:00 GMT  
 Writing to File Streams

Quote:

>I drafted a method for adding text into a file stream directly. Rather than
>taking the data from the file stream, placing it in an array, then amending
>it and placing it back in it's file stream. The method ( to my own
>surprise ), actually worked,
>but it places the new text over the first line
>of the file as it was before, not much use.
>Although knowing the answer may prove quite useless, I am curious.
>How do I control the placing / positioning of text into a file stream,
>without having an array in the middle of the process ?

Well, if you want to glue more stuff at the _end_ of a file, try
opening for append (check class methods for File); or use the
positioning msgs in the other posts to ensure that your
new text will grow at the file's end instead of overwriting
characters at the beginning.

If you want to insert arbitrary strings into the beginning of the
file, for example where this...
   ---myfile.dat---
   oldLineA
   oldLineB
   ----------------

becomes this...
   ---myfile.dat---
   newLineA
   oldLineA
   oldLineB
   ----------------

then I think you will, one way or another, need to
completely recreate the file.

The easiest way is, like you mentioned, slurping the
original into an Array (though an OrderedCollection
would probably be easier to manipulate), tweak the
collection, then dump it back out.
cons to this: large files = big memory requirements, and if
somebody kicks your power cord out of the wall during the
write phase, only part of your data might make it to disk
(while the rest of it evaporates from ram).

A marginally better approach would be smth like this:

addTo: aFile for: aString
   | in out character |
   in := File pathName: aFile.
   out := File pathName: 'temp.dat'.
   out nextPutAll: aString; cr.
   [ (character := in next) notNil ] whileTrue: [ out nextPut: character ].
   in close.
   out close.
   File delete: aFile.
   File rename: 'temp.dat' to: aFile.

There are certainly better ways to do this.  For example, Files
in your environment probably know how to add all of themselves
to another file (similar to the #addAll: msg that collections
know about).  And I'd be surprised if I have the methods for
killing & moving files around correct for your environment.
But I would expect that to be some class methods somewhere
that do that sort of thing.

So the short answer woudl be it is easier if you can glue
the new strings onto the end of an existing file rather than
shoehorning them in at the beginning.

fwiw - as I recall, raw text files work like this back in the
procedural land.  This is one of the reasons that writing
a good text editor gets to be non-trivial when you start
working with large amounts of text.

Quote:

>Class method for adding data to a file. Class is "Food",  ( not that it
>matters ).

>addTo: aFile for: aString
>    | outputStream |
>    outputStream := File pathName: aFile.
>    outputStream nextPutAll: aString ; cr.
>    outputStream close.

>I live in hope that I start to understand this language, but right now I
>wish the information could be uploaded to my brain like in "The Matrix".
>Thanks in anticipation.

>Kelly Tait.

You're doing exactly what you need to do in order to learn this.
Keep at it; I applaud your research!

John G.



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How to write a header with an array of u16 to a file with byte stream

2. writing 16bit bibary stream to file with 12bit steam

3. Reading/Writing files using streams

4. SQL- Stream(File)/Flush(File)

5. Conversion of Fortran File unit to C File stream

6. Stream "open write" notready error

7. write a stream to another

8. write a stream to another

9. Byte order writing to socket stream

10. Writing short-floats to binary stream?

11. reading and writing to/from binary streams

12. Writing to TCL_STDOUT using c++ streams...

 

 
Powered by phpBB® Forum Software