Would someone mind taking a look and maybe explaining this piece of code?

Well the title says it all. 

local number \_path = system.pathForFile( "number.txt", system.ResourceDirectory ) \_file = io.open( \_path, "r" ) for line in \_file:lines() do number = tonumber (line) end io.close( \_file ) \_file = nil \_path = nil \_path = system.pathForFile( "number.txt", system.DocumentsDirectory ) \_file = io.open( \_path, "r" ) if not (\_file) then \_path = system.pathForFile( "number.txt", system.DocumentsDirectory ) \_file = io.open( \_path, "w" ) \_file:write( number ) end io.close( \_file ) \_file = nil \_path = nil

What i do know is this loads up the number.txt to be able to read and write to it?

Thanks!

–SonicX278 

I think rather than digesting some code, it’d be better to understand how the I/O library works with system.pathForFile and such. With that, here’s working code for reading from a file:

local path = system.pathForFile("fileName.txt", system.ResourceDirectory) local file = io.open(path, "r") -- Use "r" for read mode; use "w" for write mode. Write mode will create the file if it does not exist, but read mode will not. local contents if not file then error("Failed to read file.") else contents = file:read("\*a") -- Read everything from the file io.close(file) -- Close the file when done to free memory end print("The file contents are:") print(contents)

To write to a file, you’d use io.open(path, “w”) and file:write() instead.

Further reading: https://docs.coronalabs.com/daily/guide/data/readWriteFiles/index.html

  • Caleb

What about this part?

 for line in file:lines() do number = tonumber(line) end

–SonicX278 

That’s a Lua I/O line iterator. It’s just a for loop. It gives you each line in the file. Then the tonumber() function converts that line to a number and stores it in the variable “number”. Note that this variable will be overwritten each time the loop runs, so it’ll finally end up that “number” will be set to the last line of the file as a number.

You can find a huge amount of information on that iterator if you just try Googling around for stuff like “lua file:lines()” or even that first line - “for line in file:lines() do”.

  • Caleb

So look. Am i understanding this correctly?

local number \_path = system.pathForFile( "number.txt", system.ResourceDirectory ) --Get \_path destination. \_file = io.open( \_path, "r" ) --Open the \_path and read whats in it. for line in \_file:lines() do --Get the number that was in number.txt and give it to the "number" variable. number = tonumber (line) end io.close( \_file )--Close \_file. \_path = system.pathForFile( "number.txt", system.DocumentsDirectory ) --Get new directory. \_file = io.open( \_path, "r" ) --Open and read the file. if not (\_file) then --If above doesn't exist then create it and write the number variable into it. \_path = system.pathForFile( "number.txt", system.DocumentsDirectory ) \_file = io.open( \_path, "w" ) \_file:write( number ) end io.close( \_file )--Close \_file.

Please go over the comments in this code and tell my if i’m correct. ( It’ll make more sense if put into an editor like Sublime )

–SonicX278 

That looks correct; just one note - io.open(path, “r”) does not mean “open path and read what’s in it.” It just means “open path for reading”. You’re only reading the file when you do the line iterator. You can also read it with my code above - via file:read("*a").

  • Caleb

So i had the correct idea. Thanks! 

–SonicX278 

it copy only the last line of a file that is in ResourceDirectory and copy it to a new file in Document directory.

example, if you have a file number.txt with:

1

2

3

4

5

destination file will have inside:

5

you can copy all content of the original file with less lines. please note that i don’t recommend this code, is just for fun, how you can do more with less lines :wink:

local \_path = system.pathForFile( "number.txt", system.ResourceDirectory ) local \_path\_destination = system.pathForFile( "number.txt", system.DocumentsDirectory ) \_file = io.open( \_path\_destination, "w+" ) for line in io.lines(\_path) do \_file:write(tonumber(line).."\n") end io.close( \_file )

Yes I know. I did everything like it needed to be in the plugin. --SonicX278

I was making a plugin for a couple projects i was working on and i had the code i was posting above i just didn’t understand it very well. But after about an hour i got the whole thing through my head thanks to @Caleb.

–SonicX278 

I think rather than digesting some code, it’d be better to understand how the I/O library works with system.pathForFile and such. With that, here’s working code for reading from a file:

local path = system.pathForFile("fileName.txt", system.ResourceDirectory) local file = io.open(path, "r") -- Use "r" for read mode; use "w" for write mode. Write mode will create the file if it does not exist, but read mode will not. local contents if not file then error("Failed to read file.") else contents = file:read("\*a") -- Read everything from the file io.close(file) -- Close the file when done to free memory end print("The file contents are:") print(contents)

To write to a file, you’d use io.open(path, “w”) and file:write() instead.

Further reading: https://docs.coronalabs.com/daily/guide/data/readWriteFiles/index.html

  • Caleb

What about this part?

 for line in file:lines() do number = tonumber(line) end

–SonicX278 

That’s a Lua I/O line iterator. It’s just a for loop. It gives you each line in the file. Then the tonumber() function converts that line to a number and stores it in the variable “number”. Note that this variable will be overwritten each time the loop runs, so it’ll finally end up that “number” will be set to the last line of the file as a number.

You can find a huge amount of information on that iterator if you just try Googling around for stuff like “lua file:lines()” or even that first line - “for line in file:lines() do”.

  • Caleb

So look. Am i understanding this correctly?

local number \_path = system.pathForFile( "number.txt", system.ResourceDirectory ) --Get \_path destination. \_file = io.open( \_path, "r" ) --Open the \_path and read whats in it. for line in \_file:lines() do --Get the number that was in number.txt and give it to the "number" variable. number = tonumber (line) end io.close( \_file )--Close \_file. \_path = system.pathForFile( "number.txt", system.DocumentsDirectory ) --Get new directory. \_file = io.open( \_path, "r" ) --Open and read the file. if not (\_file) then --If above doesn't exist then create it and write the number variable into it. \_path = system.pathForFile( "number.txt", system.DocumentsDirectory ) \_file = io.open( \_path, "w" ) \_file:write( number ) end io.close( \_file )--Close \_file.

Please go over the comments in this code and tell my if i’m correct. ( It’ll make more sense if put into an editor like Sublime )

–SonicX278 

That looks correct; just one note - io.open(path, “r”) does not mean “open path and read what’s in it.” It just means “open path for reading”. You’re only reading the file when you do the line iterator. You can also read it with my code above - via file:read("*a").

  • Caleb

So i had the correct idea. Thanks! 

–SonicX278 

it copy only the last line of a file that is in ResourceDirectory and copy it to a new file in Document directory.

example, if you have a file number.txt with:

1

2

3

4

5

destination file will have inside:

5

you can copy all content of the original file with less lines. please note that i don’t recommend this code, is just for fun, how you can do more with less lines :wink:

local \_path = system.pathForFile( "number.txt", system.ResourceDirectory ) local \_path\_destination = system.pathForFile( "number.txt", system.DocumentsDirectory ) \_file = io.open( \_path\_destination, "w+" ) for line in io.lines(\_path) do \_file:write(tonumber(line).."\n") end io.close( \_file )

Yes I know. I did everything like it needed to be in the plugin. --SonicX278