Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

reading file to a variable
Started by ttee Jun 22 2013 11:23 PM

- - - - -
2 replies to this topic
variable string file line
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

ttee

[GLOBAL: userInfoPane.html]
ttee
  • Observer

  • 12 posts
  • Corona SDK

    local contents = ""
    local file = io.open( path, "r" )
    if (file) then
        -- read all contents of file into a string
        contents = file:read()
        file:close()
 
--        io.close( file )
        print(contents)
 
    else
        print("file not found")
    end
        print(path)
 
Hi all,
my file i am reading is not empty, but i am get nil when i print (contents)
would appreciate any help.
thanks,
ttee


[TOPIC: post.html]
#2

lano78

[GLOBAL: userInfoPane.html]
lano78
  • Contributor

  • 370 posts
  • Corona SDK

you haven´t specified the path.

local path = system.pathForFile(fileName, fileDirectory)



[TOPIC: post.html]
#3

ttee

[GLOBAL: userInfoPane.html]
ttee
  • Observer

  • 12 posts
  • Corona SDK

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.

 

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



[topic_controls]
 
[/topic_controls]