Open,write,read file

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
oldummy
Posts: 78
Joined: Mon Jun 13, 2016 3:21 pm

Open,write,read file

Post by oldummy » Sat Jan 13, 2024 1:54 pm

I'm having yet another issue. This time with opening,writing and closing a file. The file resides in the same folder as my stack. I cannot seem to write to it. Whenever I open the file in notepad after I try it, it contains no text.
I suppose if my first script doesn't work, the second one will not either.

Code: Select all

on mouseUp
   open file "myletter"for write--I tried this without the"for write" as well. 
   write "I like strawberry pie to file "myletter"
   close file "myletter"
end mouseUp
   
--on mouseup
--   open file "myletter"
--   read from file "myletter" until eof
--   close file "myletter"
--   put it into fld"wert"
--end mouseup

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Open,write,read file

Post by richmond62 » Sat Jan 13, 2024 2:03 pm

Perhaps your trouble is that you don't have double quotes after 'strawberry pie'.

Code: Select all

write "I like strawberry pie" to file "myletter"
Um, No, that didn't work . . . just tried it. :cry:
Last edited by richmond62 on Sat Jan 13, 2024 2:06 pm, edited 1 time in total.

oldummy
Posts: 78
Joined: Mon Jun 13, 2016 3:21 pm

Re: Open,write,read file

Post by oldummy » Sat Jan 13, 2024 2:05 pm

Thank you. I was just signing back in to say I did find the problem, and yes it was ME!
Again,thanks.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Open,write,read file

Post by richmond62 » Sat Jan 13, 2024 2:09 pm

Actually that code DID work, as I just found a file called "myLetter" INSIDE the LC bundle (here, on MacOS 12) containing "I like strawberry pie".

I was being stupid enough to think that the file would be somewhere discoverable without having to see the package contents of LiveCode.

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Open,write,read file

Post by stam » Sat Jan 13, 2024 4:50 pm

oldummy wrote:
Sat Jan 13, 2024 1:54 pm

Code: Select all

on mouseUp
   open file "myletter"for write--I tried this without the"for write" as well. 
   ...
I think you need to provide either a filepath instead of a file name, or you need to designate the defaultFolder -- otherwise it defaults to the last used DefaultFolder or the app/engine.



The dictionary is helpful and clear:
Dictionary on 'Open File' wrote:open file <filePath> [for [ {[<encoding>] text | binary}] {update | read | write | append}]

filePath
Specifies the name and location of the file you want to open or create. If you specify a name but not a location, LiveCode assumes the file is in the defaultFolder. If the file you specify doesn't exist, LiveCode creates it unless the containing folder you specify also doesn't exist, in which case the command fails and the result function returns "Can't open that file.".
Dictionary on DefaultFolder wrote: When a LiveCode application starts up, the defaultFolder initially contains whatever the working directory was at the time the application was launched. Typically this is the folder that the application resides in, however this is not always the case and shouldn't be relied on. It is best always to set the defaultFolder before using relative paths.
Also, if opening a file for write, don't forget:
Important: Before opening any file for writing, remember to back up the file contents as LiveCode will erase them even if you do not write to the file after opening it.
So use with caution and use 'open file for append | update' if editing a file.

Perhaps even better just use the get/put URL syntax instead... eg

Code: Select all

put URL ("file:/" & <filepath>) into <text>

-- edit variable/field then

put <text> into URL ("file:/" & <filepath>) 

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”