Can't overwrite a file (Windows 10)

Is there some kind of a problem with Windows 10, Corona and file writing?

If the file doesn’t exists, the file is written. If the file exists, I’ll get an error message:

attempt to index local 'file' (a nil value)

Code:

local path = system.pathForFile( "gameData.json", system.DocumentsDirectory ) local file = io.open(path, "w") file:write( "Hi!") io.close( file )

Tried also with “w+”

When testing with Mac, everything works.

Corona version 2015.2731 (2015.10.5)
 

Try using my io extensions instead of rolling your own, or dig through them and see if my code is different from yours:

http://github.com/roaminggamer/SSKCorona/blob/master/ssk/extensions/io.lua

If you use the file, save it in root as io_ext.lua or as io.lua a folder called extensions, then require it like this:

require "io\_ext" -- OR require "extensions.io"

Here is a project with all the extensions you need for file IO and table/load save:

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2015/11/extensions.zip

Does this:

require "extensions.string" require "extensions.table" require "extensions.io" local settings = {} settings.volume = 0.5 settings.score = 10 settings.name = "Bob" settings.inventory = { "eggs", "bacon" } table.dump( settings ) table.print\_r( settings ) table.save( settings, "settings.json", system.DocumentsDirectory ) settings = nil -- clearing to prove it loads settings = table.load( "settings.json", system.DocumentsDirectory ) table.dump( settings ) table.print\_r( settings )

PS - I’m Running Windows 10 and I tested the above.  Works perfectly.  So, save time, use my stuff, and get yer game or app done.   :smiley:

Thanks roaminggamer. Don’t know what it was, 'cause now everything works with my original codes. I think Windows temporarily locked the file/folder somehow… I used this code: https://github.com/robmiracle/Simple-Table-Load-Save-Functions-for-Corona-SDK

Where was the game folder located?  You’re not keeping in under one of the ‘program’ folders are you?

If so, you don’t want to do that.  Keep it in a documents folder, desktop sub-folder, or separate data drive.

Try using my io extensions instead of rolling your own, or dig through them and see if my code is different from yours:

http://github.com/roaminggamer/SSKCorona/blob/master/ssk/extensions/io.lua

If you use the file, save it in root as io_ext.lua or as io.lua a folder called extensions, then require it like this:

require "io\_ext" -- OR require "extensions.io"

Here is a project with all the extensions you need for file IO and table/load save:

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2015/11/extensions.zip

Does this:

require "extensions.string" require "extensions.table" require "extensions.io" local settings = {} settings.volume = 0.5 settings.score = 10 settings.name = "Bob" settings.inventory = { "eggs", "bacon" } table.dump( settings ) table.print\_r( settings ) table.save( settings, "settings.json", system.DocumentsDirectory ) settings = nil -- clearing to prove it loads settings = table.load( "settings.json", system.DocumentsDirectory ) table.dump( settings ) table.print\_r( settings )

PS - I’m Running Windows 10 and I tested the above.  Works perfectly.  So, save time, use my stuff, and get yer game or app done.   :smiley:

Thanks roaminggamer. Don’t know what it was, 'cause now everything works with my original codes. I think Windows temporarily locked the file/folder somehow… I used this code: https://github.com/robmiracle/Simple-Table-Load-Save-Functions-for-Corona-SDK

Where was the game folder located?  You’re not keeping in under one of the ‘program’ folders are you?

If so, you don’t want to do that.  Keep it in a documents folder, desktop sub-folder, or separate data drive.