How to read a specific file using corona sdk

I use to program although it has been quite a few years so Im having to relearn most of it, Im trying to make it so when one of the 3 buttons are clicked it reads the file attached to the button but all i get at the moment is nul in the terminal when i run it

Ive tried moving around sections of code but I cant find anything that works

local centerX = display.contentCenterX local centerY = display.contentCenterY local background = display.newImage("background.png") background.x = centerX background.y = centerY local widget = require( "widget" ) -- Path for the file to read local cluthaDirect = system.pathForFile( "clutha\_Run.txt" ) local ranfDirect = system.pathForFile( "ranf\_Run.txt" ) local centDirect = system.pathForFile( "cent\_Run.txt" ) local function cluthaButtonEvent(event) local phase = event.phase if "ended" == phase then print(cluthaFile) end end local function centButtonEvent(event) local phase = event.phase if "ended" == phase then print("C E N T R A L") end end local function ranfButtonEvent(event) local phase = event.phase if "ended" == phase then print("R A N F U R L Y") end end local cluthaButton = widget.newButton { left = centerX - 60, top = centerY - centerY, width = display.contentWidth/2, height = 60, defaultFile = "buttonUnpressed.png", overFile = "buttonPressed.png", label = "clutha", onEvent = cluthaButtonEvent, } local centButton = widget.newButton { left = centerX - 60, top = centerY - centerY + 80, width = display.contentWidth/2, height = 60, defaultFile = "buttonUnpressed.png", overFile = "buttonPressed.png", label = "central", onEvent = centButtonEvent, } local ranfButton = widget.newButton { left = centerX - 60, top = centerY - centerY + 160, width = display.contentWidth/2, height = 60, defaultFile = "buttonUnpressed.png", overFile = "buttonPressed.png", label = "ranf", onEvent = ranfButtonEvent, } -- Path for the file to read --local cluthaDirect = system.pathForFile( "clutha\_Run.txt" ) --local ranfDirect = system.pathForFile( "ranf\_Run.txt" ) --local centDirect = system.pathForFile( "cent\_Run.txt" ) -- Open the file handle local cluthaFile, errorString = io.open( cluthaDirect, "r" ) local centFile, errorString = io.open( centDirect, "r" ) local ranfFile, errorString = io.open( ranfDirect, "r" ) if not cluthaFile then -- Error occurred; output the cause print( "cluthaFile error: " .. errorString ) else -- Output lines for line in cluthaFile:lines() do print( line ) end -- Close the file handle --io.close( cluthaFile ) end cluthaFile = nil

https://docs.coronalabs.com/api/type/File/read.html

Hello, ive looked at the documentation but im still unsure what im doing wrong

any ideas?

thanks

Looking at your source code, there are quite a few issues going on.

First your buttons don’t do anything more than print a message to the console log. And this function will print nil:

local function cluthaButtonEvent(event) local phase = event.phase if "ended" == phase then print(cluthaFile) end end

Because Lua is a single pass compiler, cluthaFile doesn’t exist yet, so you’re in effect using the code: “print( nil )”. cluthaFile has to be defined higher up in your code.

Next, Corona is event-driven, that means the code in your program will execute from top to bottom and stop when the bottom is hit. After that, activity happens when some event occurs, like touching/clicking your buttons. Thus, the way your code is structured, it’s going to attempt to read files after it draws the buttons on the screen, but not in reaction to buttons being touched.

You define a path to a file:

local cluthaDirect = system.pathForFile( "clutha\_Run.txt" )

But you don’t specify where the file exists. Corona uses a sandbox model where there are specific folders that apps are allowed to read and write from. The system.pathForFile() defaults to system.ResourceDirectory() (see: http://docs.coronalabs.com/api/library/system/pathForFile.html) Since you specify no other path information, this is going to look for the file clutha_Run.txt in the same folder that main.lua is in. It is unclear from your code that the file exists in that location.

If it exists, this block of code should work, sort of…

if not cluthaFile then -- Error occurred; output the cause print( "cluthaFile error: " .. errorString ) else -- Output lines for line in cluthaFile:lines() do print( line ) end -- Close the file handle --io.close( cluthaFile ) end cluthaFile = nil

If the file can’t be opened, you should be getting a message in your console log window as to why. We don’t have that information to help you. If it successfully being opened, then it should dump the contents of the file to the console log. You do need to uncomment that io.close() statement.

But because this happens outside of any event handler, it’s just going to dump the file to the console log and then when you click on buttons, just print out those print statements. You also make no attempts to read the other two files.

If you want the buttons to do the work, you should create a function before you create the buttons that can read a file and print it to the console log (or do whatever you want with the red text).

There is a programming concept called DRY - Don’t repeat yourself. If it were me I would create a single file reading function, and pass the filename to that function that opens and reads the file.

Then your button code would call the function passing the name of the file to the file reading function…

Rob

https://docs.coronalabs.com/api/type/File/read.html

Hello, ive looked at the documentation but im still unsure what im doing wrong

any ideas?

thanks

Looking at your source code, there are quite a few issues going on.

First your buttons don’t do anything more than print a message to the console log. And this function will print nil:

local function cluthaButtonEvent(event) local phase = event.phase if "ended" == phase then print(cluthaFile) end end

Because Lua is a single pass compiler, cluthaFile doesn’t exist yet, so you’re in effect using the code: “print( nil )”. cluthaFile has to be defined higher up in your code.

Next, Corona is event-driven, that means the code in your program will execute from top to bottom and stop when the bottom is hit. After that, activity happens when some event occurs, like touching/clicking your buttons. Thus, the way your code is structured, it’s going to attempt to read files after it draws the buttons on the screen, but not in reaction to buttons being touched.

You define a path to a file:

local cluthaDirect = system.pathForFile( "clutha\_Run.txt" )

But you don’t specify where the file exists. Corona uses a sandbox model where there are specific folders that apps are allowed to read and write from. The system.pathForFile() defaults to system.ResourceDirectory() (see: http://docs.coronalabs.com/api/library/system/pathForFile.html) Since you specify no other path information, this is going to look for the file clutha_Run.txt in the same folder that main.lua is in. It is unclear from your code that the file exists in that location.

If it exists, this block of code should work, sort of…

if not cluthaFile then -- Error occurred; output the cause print( "cluthaFile error: " .. errorString ) else -- Output lines for line in cluthaFile:lines() do print( line ) end -- Close the file handle --io.close( cluthaFile ) end cluthaFile = nil

If the file can’t be opened, you should be getting a message in your console log window as to why. We don’t have that information to help you. If it successfully being opened, then it should dump the contents of the file to the console log. You do need to uncomment that io.close() statement.

But because this happens outside of any event handler, it’s just going to dump the file to the console log and then when you click on buttons, just print out those print statements. You also make no attempts to read the other two files.

If you want the buttons to do the work, you should create a function before you create the buttons that can read a file and print it to the console log (or do whatever you want with the red text).

There is a programming concept called DRY - Don’t repeat yourself. If it were me I would create a single file reading function, and pass the filename to that function that opens and reads the file.

Then your button code would call the function passing the name of the file to the file reading function…

Rob