“ChatGPT” wrote:
There are a few ways to do file I/O in LiveCode, but the simplest is to use the put ... into URL ("file:...") form.
⸻
Example: Save and Load from a Text File
Suppose you have a field on your card called fld "MyData".
To save the contents of the field to a text file:
Code: Select all
on mouseUp
-- pick a location and name
answer file "Save data as:"
if it is empty then exit mouseUp
put field "MyData" into URL ("file:" & it)
end mouseUp
To load it back later:
Code: Select all
on mouseUp
answer file "Choose a file to open:"
if it is empty then exit mouseUp
put URL ("file:" & it) into field "MyData"
end mouseUp
What’s happening here:
• answer file shows a standard open/save dialog so the user can pick a file.
• put field "MyData" into URL ("file:" & it) writes the field contents directly to the file.
• put URL ("file:" & it) into field "MyData" reads the file back into the field.
⸻
A fixed-name file (no dialogs)
If you always want to use the same file (say, on desktop):
Code: Select all
-- Save
put specialFolderPath("desktop") & "/myData.txt" into tFile
put field "MyData" into URL ("file:" & tFile)
-- Load
put specialFolderPath("desktop") & "/myData.txt" into tFile
put URL ("file:" & tFile) into field "MyData"
Here’s the “under-the-hood” way to do file I/O in LiveCode using open file, read, write, and close file. I’ll show save, load, and append patterns, plus basic error handling.
⸻
Save (overwrite) the contents of a field to a text file
Code: Select all
command SaveFieldToFile pFieldName
-- Pick/save to a file
answer file "Save data as:"
if it is empty then exit SaveFieldToFile
put it into tPath
-- Open for write (creates or truncates)
open file tPath for write
if the result is not empty then
answer error "Couldn't open file:" && the result
exit SaveFieldToFile
end if
-- Write the text and close
put field pFieldName into tText
write tText to file tPath
close file tPath
end SaveFieldToFile
Load (read) a text file into a field — line by line
Code: Select all
command LoadFileIntoField pFieldName
answer file "Choose a file to open:"
if it is empty then exit LoadFileIntoField
put it into tPath
open file tPath for read
if the result is not empty then
answer error "Couldn't open file:" && the result
exit LoadFileIntoField
end if
put empty into tBuffer
repeat until eof(tPath)
read from file tPath for 1 line
put it after tBuffer
end repeat
close file tPath
put tBuffer into field pFieldName
end LoadFileIntoField
Append a single line to an existing file (or create it)
Code: Select all
command AppendLineToFile pLine
answer file "Pick a file to append to:"
if it is empty then exit AppendLineToFile
put it into tPath
open file tPath for append
if the result is not empty then
answer error "Couldn't open file for append:" && the result
exit AppendLineToFile
end if
write pLine & cr to file tPath
close file tPath
end AppendLineToFile
Read a file in fixed-size chunks (useful for big files)
Code: Select all
command LoadFileChunked pFieldName
answer file "Choose a file to open:"
if it is empty then exit LoadFileChunked
put it into tPath
open file tPath for read
if the result is not empty then
answer error "Couldn't open file:" && the result
exit LoadFileChunked
end if
put empty into tBuffer
repeat until eof(tPath)
read from file tPath for 4096 -- bytes
put it after tBuffer
end repeat
close file tPath
put tBuffer into field pFieldName
end LoadFileChunked
Read a file in fixed-size chunks (useful for big files)
Code: Select all
command LoadFileChunked pFieldName
answer file "Choose a file to open:"
if it is empty then exit LoadFileChunked
put it into tPath
open file tPath for read
if the result is not empty then
answer error "Couldn't open file:" && the result
exit LoadFileChunked
end if
put empty into tBuffer
repeat until eof(tPath)
read from file tPath for 4096 -- bytes
put it after tBuffer
end repeat
close file tPath
put tBuffer into field pFieldName
end LoadFileChunked
Notes & tips
• Check errors with the result: After open, read, or write, if the result is not empty, an error occurred (e.g., permissions, locked file, bad path).
• Line endings: read ... for 1 line reads up to the next line break; write ... doesn’t add a line break unless you add cr.
• Overwrite vs append:
• open ... for write truncates/creates the file.
• open ... for append writes at the end.
• open ... for read is read-only.
• Binary data: Use the binfile: form with the URL syntax, or still use open file but treat content as bytes. For pure text, the above is fine.
• UTF-8 text: The open/write form writes raw bytes in the engine’s default encoding. If you need UTF-8 reliably, the simplest approach is:
Code: Select all
put textEncode(field "MyData","UTF-8") into tUTF8
put tUTF8 into URL ("file:" & tPath) -- URL form writes bytes directly
And to read back:
Code: Select all
put URL ("file:" & tPath) into tBytes
put textDecode(tBytes,"UTF-8") into field "MyData"
Common path helpers: specialFolderPath("documents"), specialFolderPath("desktop"), etc.