reading from / writing to file : 2 dumb questions

Peter Haworth pete at lcsql.com
Tue Sep 9 14:33:28 EDT 2014


>From the dictionary:

"Use the for update form to open the file for both reading and writing. If
the file is opened for update, you can use both the read from file and
write to file commands to examine or change it, and writing to the file
places the data at the specified position without replacing other
characters beyond the written data."

"If you do not specify a form, the file is opened for update."

In your first case, the read causes the "specified position"  to be the end
of the file so your data is appended at that point.  In the second case,
you don't read anything after the second open so the "specified position"
is the start of the file so your write overwrites what is already there.

You can control all of this by specifying a mode when you open the file,
depending on what your needs are.



Pete
lcSQL Software <http://www.lcsql.com>
Home of lcStackBrowser <http://www.lcsql.com/lcstackbrowser.html> and
SQLiteAdmin <http://www.lcsql.com/sqliteadmin.html>


On Tue, Sep 9, 2014 at 9:58 AM, <jbv at souslelogo.com> wrote:

> Richmond,
> Thanks for your reply.
> Yes, I perfectly understand what happens, but my point is :
> why is it so ? What is the logic behind ?
> Is it specific to LC of is it closely related to the different OSes
> on which LC runs ?
> Why the fact of closing then reopening a file leads to overwriting
> its content ?
>
> Best,
> jbv
>
>
> > On 09/09/14 18:39, jbv at souslelogo.com wrote:
> >> Hi list
> >>
> >> I am trying to improve the speed of a large script including
> >> many loops.
> >> Could anyone explain why :
> >>
> >> - question 1 : if a text file contains "toto", after running the
> >> following lines
> >>     open file tpath
> >>     read from file tpath until EOF
> >>     write (it & return & "titi") to file tpath
> >>     close file tpath
> >>
> >> the file contains :
> >>    totototo
> >>    titi
> > Because you are doing this:
> >
> > reading the contents of file tpath
> >
> > and THEN writing that ('toto') and 'titi' back to the file.
> >
> > So 'toto' + 'toto' + return + 'titi' is what you end up with.
> > ---------------------------------------------------------------
> >>
> >> and when running these lines
> >>     open file tpath
> >>     read from file tpath until EOF
> >>     close file tpath
> >>     open file tpath
> >>     write (it & return & "titi") to file tpath
> >>     close file tpath
> >>
> >> the file contains :
> >>     toto
> >>     titi
> >
> > Because here you close the file for reading and then open it again.
> >
> > In the first case your information is appended to the existing file.
> >
> > In the second case your information overwrites the existing file.
> > -----------------------------------------------------------------
>
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>



More information about the use-livecode mailing list