write results on a file

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

write results on a file

ale-2
Hi, i'm just starting to learn smalltalk.
i'd like to know how to write the result of any computation that i
write on a workspace, directly on a file.
thanks


Reply | Threaded
Open this post in threaded view
|

Re: write results on a file

Ian Bartholomew-21
ale,

> Hi, i'm just starting to learn smalltalk.

Welcome, you've made a good choice :-)

> i'd like to know how to write the result of any computation that i
> write on a workspace, directly on a file.
> thanks

You create an instance of FileStream and then write to that.  As in

fs := FileStream write: 'TheFileName.txt'.
[ | x |
        x := 100 factorial.
        fs
                nextPutAll: 'The factorial of 100 is ';
                print: x;
                cr] ensure: [fs close]

The #ensure: block is not really necessary but makes sure that if there
is an error in your code you are not left with dangling open files.
Without the #ensure block you could just have

fs := FileStream write: 'TheFileName.txt'.
x := 100 factorial.
fs
        nextPutAll: 'The factorial of 100 is ';
        print: x;
        cr.
fs close

--
Ian

Use the Reply-To address to contact me.
Mail sent to the From address is ignored.