reading from / writing to file : 2 dumb questions

Jacques Hausser jacques.hausser at unil.ch
Tue Sep 9 13:22:46 EDT 2014


Think you have a pointer to the file; when you open a file, the pointer is on the first char. When you read the file till EOF, the pointer stays just after the last "readable" char. So, if you immediately write something, the writing weill start here.
Now, if you close the file and reopen it, the pointer will be at the beginning again. And you'll overwrite what's on the file, except if you < write return & "titi" to file tFile at 5 (since toto has four chars).

J.

Le 9 sept. 2014 à 18:58, jbv at souslelogo.com a écrit :

> 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

******************************************
Prof. Jacques Hausser
Department of Ecology and Evolution
Biophore / Sorge
University of Lausanne
CH 1015 Lausanne
please use my private address:
6 route de Burtigny
CH-1269 Bassins
tel: 	++ 41 22 366 19 40
mobile:	++ 41 79 757 05 24
E-Mail:	jacques.hausser at unil.ch
*******************************************





More information about the use-livecode mailing list