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."
print(file)
writeTest(file,txt)  -- comment this line and restart to check the file is there
readTest(file)

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
readTest(file)

-- binary test
print("--------- test 2 ---------------")
print("--- binary read test ----")
print("--- from text file :")
test = readBinary(file)
print(table.concat(test,"-"))



end

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



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


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

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    
end

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: