Code from sample does not work. File:write()

Hello!

Today I decided to learn how to work with files. I opened corona-blog, and saw the next code

local saveData = "My app state data" local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory ) local file = io.open( path, "w" ) file:write( saveData ) io.close( file ) file = nil

Which is very simple. But Emulator said that something wrong and gave me the next line:

Runtime error d:\corona\project\file\main.lua attemt to index local 'file' (a nil value)

And show that something wrong in the file:write( saveData ) string.

Also I started the FileDemo example and it said the same.

Is it something wrong on my computer, or I just mist something?

That code looks fine to me.  My only thought would be that the folder where the Corona Simulator is attempting to write the file is read-only, so that’s why it’s failing.  From the Simulator, click File | Show Project Sandbox, and check whether the folder is for some reason set to read-only on your machine.

  • Andrew

Thanks, I think the reason is excactly you told. My AppData folder flaged as read-only. So the next question, how to unflug it? Because I tried in the properties of the folder, unchecked flag, but after apply it became read-only again.

Cool, well at least it seems like we’ve identified the issue.  I don’t know why it keeps going back to read-only though – you might have to look through the Windows knowledge base for that.

  • Andrew

That code looks fine to me.  My only thought would be that the folder where the Corona Simulator is attempting to write the file is read-only, so that’s why it’s failing.  From the Simulator, click File | Show Project Sandbox, and check whether the folder is for some reason set to read-only on your machine.

  • Andrew

Thanks, I think the reason is excactly you told. My AppData folder flaged as read-only. So the next question, how to unflug it? Because I tried in the properties of the folder, unchecked flag, but after apply it became read-only again.

Cool, well at least it seems like we’ve identified the issue.  I don’t know why it keeps going back to read-only though – you might have to look through the Windows knowledge base for that.

  • Andrew