Read/write files

I last used Codea almost 2 years ago–so much has changed! At that time one couldn’t read or write text files, or serialize to them. Can you do this now? I didn’t understand why “notepad” apps could, but Codea couldn’t. It made it nearly useless for my application back then. (I know about “project data”–inadequate.) If it can do it now, great. And if so, what tool is recommended to get the text files back and forth to a PC? Also, is it easier to move entire projects back and forth now, or is Apple still making that difficult? One last question: if Codea is “hung” while executing a program, is there a way to recover (besides killing the program)? Some old projects I had no longer work, they just hang the system, not sure what changed.

How is project data inadequate? What specifics are you needing with writing files? You can write files and save to droppbox. I save pretty much everything to project data. You can serialize the data and save. I’ve even saved data using jpg. As far as transfering you can use AutoGist to put your projects up on github and pull them down on other clients or via computer. It wouldnt be hard to write an app that would update all clients to the same code. If you can let me know what specificly you are looking for, I’d be glad to help out.

So you’re saying the Lua I/O functions work to create text files now?

It does. And binary too. Try that one:

--# Main
-- test io

-- Use this function to perform your initial setup
function setup()

print("--------- test 1 ---------------")
-- this works
local file = os.getenv("HOME").."/Documents/my.txt"
local txt = "This is\
some sample text\
for Lua."
writeTest(file,txt)  -- comment this line and restart to check the file is there

print("--------- test 2 ---------------")
-- this SEEMS to work, because read works, but the file is actually not writen on disk
-- if the program is run again with the write commented, then nothing reads.
-- so you cam write in root folder, but not subfolders
file = os.getenv("HOME").."/Documents/test_io_simple.codea/my.txt"
writeTest(file,txt)  -- comment this line and restart to check the file is there

-- binary test
print("--------- test 2 ---------------")
print("--- binary read test ----")
print("--- from text file :")
test = readBinary(file)


function writeTest(file,txt)
    print("------------------------ ")
    print("write in "..file)
    print("---- write done ----")
function readTest(file)
    print("------------------------ ")
    print("read from "..file)
    local li
    for i in io.lines() do print(i) end
    -- no close needed: done by io.lines()
    print("---- read done ----")

-- found on web
function readBinary(file)
    local hBinaryFile =, "rb");
    local sBinaryContent = "";
            local block = hBinaryFile:read(512); -- Read only 512 bytes per iteration
            if(not block)then break;end
            sBinaryContent = sBinaryContent..block
    local t = {}
    for i=1,sBinaryContent:len() do
        t[i] = sBinaryContent:byte(i)
    return t
function writeBinary(file,sBinaryContent)
    -- Now lets output it again.
    local hBinaryOutput =, "wb");
         hBinaryOutput:write(sBinaryContent); --You could do it in parts, but oh.

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness

    -- Do your drawing here

Therefore one can read/write to any file in the directory os.getenv(“HOME”)…“/Documents/” but not any subdirectory of Documents. That should be good enough for my purposes. Next question: how do I get the files back and forth to the PC?

You can too use os.getenv(“HOME”)…“/Documents/Dropbox.spritepack/” and sync dropbox ; Or use iFunBox (or something like that) for look into ipad and get the file what you want.

Or you can use http.request to send - receive files via the web

This seems to come up again and again - nice concise response chaps.

Admins, Can we add this to the Wiki and file under ‘really useful stuff’ ? :slight_smile:

@andymac3d the wiki is a wiki … editable by anyone, so go ahead!

Thanks @Andrew_Stacey , was unaware anyone could edit the Wiki :wink: