reading file to a variable

[lua]

    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)

[/lua]

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

you haven´t specified the path.

[lua]local path = system.pathForFile(fileName, fileDirectory)[/lua]

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]

you haven´t specified the path.

[lua]local path = system.pathForFile(fileName, fileDirectory)[/lua]

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]