File io problem

@sim Since you got rid of the Dropbox folder, I created an external folder called DropboxA that contains my original Dropbox files. I’ve been going thru my projects and replacing the old Dropbox to work with DropboxA. So far everything was going ok until I got to my File.io projects. It seems that I can’t create a file using the io.open “w” command if the file doesn’t exist. See the below code. If the file test1 exists, everything works ok. If the file doesn’t exists, I get an error. I tried changing the asset.DropboxA line every way I could think of, but I still can’t get it to work. Is there a problem or haven’t I got the correct name for file= to work with an existing/non existing file.


function setup()
    -- write file
    local file=asset.DropboxA.test1.path
    local hOutput = io.open(file, "w")
    if hOutput then
        hOutput:write("this is a test")
        hOutput:close()
        print("---- write successful ----")
    else
        print("file error")
    end
end

Where did you create the DropboxA folder? Is it local (ie. in your Codea folder) or elsewhere and added as a folder reference?

If it is added as a folder reference, does it live in iCloud or on some non-local storage?

I have a feeling traditional file system APIs will not work sometimes because of this, but it would be good to explore deeper and find the issue. Once you let me know I’ll try reproducing what you have

@sim I created it using the assets add folder icon and it’s sitting in iCloud. The saveText command created the file ok.

1 Like

Perfect, thank you

I suspect it’s to do with living in iCloud. The older file operations, where you can just directly read and write files on disk (what a concept :smiley:) no longer work quite the same way when you allow file providers from arbitrary sources (USB, cloud, git, local etc)

There is an objc API for writing and reading files properly, what you would have to do is use NSFileCoordination and ensure that your read or write is lodged through the system properly, so that iOS can coordinate other processes’ access to the same file, cloud uploads and downloads, USB drive removal, and so on

Could you try saving the file with saveText instead? That function will do it the “proper” way using the operating system’s file coordination

Edit: you beat me and got to saveText first. Above is an explanation of why it is that way

@sim I don’t use file.io that often, if ever, but played with it long ago. I was just updating my projects and ran across the write problem. I could always use saveText first if I need to create a file. I was just curious why it wasn’t working.

1 Like