io.write i this version(1.5)?

Can we use the io functions in version 1.5?

I only ask because prior to the update for this version we could not. If not then when we are able to will we be able to have it write code right into a new specified tab (using the io.write function)?

1/ you can use io.* library.
2/ there are specific function in this version to write into a tab:


-- Create a tab named "Test"
-- In the current project
saveProjectTab("Test", "-- This is a test!")

Hi @jmv38,

I’m sure someone’s already demonstrated this by either putting errors or watched variable values into an added tab. Very recent since introduction of 1.5.

Bri_G

:slight_smile:

I also used !ruilov’s GitHub code to pull down entire projects into Codea; this was before the saveProjectTab() functiron existed, and it worked perfectly.

Can you guys show me where the io.write functions are documented in Codea?

I’ve tried to search them out in the in app reference, no results. Is there any reason to believe that they have been enabled prior to now?

Can someone provide an example of io.read and io.write for the iPad. I can’t seem to get them to work. I have no problem with a couple of other apps that allow the io functions.

My current understanding is



--# 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

Thanks @Jmv38 for the example. That explaines a lot and things work OK. I was using the io.read, open, and write commands like you have, but I was messing up os.getenv(). Is there a directory function or any way to look in Documents to see what files are there.

Edit: @Jmv38 I changed the line “print(table.concat(test,”-“)” to “print(table.concat(test,” “))” in – binary test and “t[i] = sBinaryContent:byte(i)” to “t[i]=string.format(”%02x",sBinaryContent:byte(i))" in function readBinary(file) so that it prints out the actual 2 byte hex values.