Thanks. i modified the code. But i am not seeing anything in the output even though my file is not empty. Here is the code.
Please could you help me see if i am missing something?
Thanks.
[lua]
display.setStatusBar(display.HiddenStatusBar)
local path = system.pathForFile( “question.txt”, system.DocumentsDirectory)
local myFile = io.open( path, “w” )
local function networkListener( event )
if ( event.isError ) then
print( “Network error!”)
elseif ( event.phase == “began” ) then
if event.bytesEstimated <= 0 then
print( “Download starting, size unknown” )
else
print( "Download starting, estimated size: " … event.bytesEstimated )
end
elseif ( event.phase == “progress” ) then
if event.bytesEstimated <= 0 then
print( "Download progress: " … event.bytesTransferred )
else
print( "Download progress: " … event.bytesTransferred … " of estimated: " … event.bytesEstimated )
end
elseif ( event.phase == “ended” ) then
print( "Download complete, total bytes transferred: " … event.bytesTransferred )
end
end
local params = {}
– This tells network.request() that we want the ‘began’ and ‘progress’ events…
params.progress = “download”
– This tells network.request() that we want the output to go to a file…
params.response = {
filename = “question.txt”,
baseDirectory = system.DocumentsDirectory
}
network.request( “http://lua-users.org/wiki/StringRecipes”, “GET”, networkListener, params )
– http://lua-users.org/wiki/FileInputOutput
– see if the file exists
function file_exists(file)
local f = io.open(file, “rb”)
if f then f:close() end
return f ~= nil
end
– get all lines from a file, returns an empty
– list/table if the file does not exist
function lines_from(file)
if not file_exists(file) then return {} end
lines = {}
for line in io.lines(file) do
lines[#lines + 1] = line
end
return lines
end
– tests the functions above
local file = system.pathForFile( “question.txt”, system.DocumentsDirectory)
local lines = lines_from(file)
– print all line numbers and their contents
for k,v in pairs(lines) do
print(‘line[’ … k … ‘]’, v)
end
[/lua]