Requesting txt file from website

My intent is to simply fetch a text file from a website and save it to a file. In order to accomplish this I tried this

function loadText()     local path = system.pathForFile( "question.txt", system.ResourceDirectory )     local myFile = io.open( path, "w+b" )           http.request{          url = "http://filesmelt.com/dl/question.txt",  sink = ltn12.sink.file(myFile),     }     print(myFile) end  

However all it prints to the terminal is

file <closed>

How could I have myFile store the same text as the downloaded .txt and how could I print that text in myFile?

Thank you, if you have any questions or need me to clarify I gladly will!

(Writing this at 4 AM, I probably sound quite foolish)

You cannot write to system.ResourcesDirectory.  Try either system.CachesDirectory or system.DocumentsDirectory

Thanks for the quick answer but the problem seems to be somewhere else. I switched system.ResourcesDirectory to system.CachesDirectory and added a tostring() to the print but it still prints

file<closed>

function loadText() &nbsp;&nbsp;&nbsp;&nbsp;local path = system.pathForFile( "question.txt", system.CachesDirectory) &nbsp;&nbsp;&nbsp;&nbsp;local myFile = io.open( path, "w" )&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;http.request{&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;url = "http://filesmelt.com/downloader/question.txt", &nbsp;sink = ltn12.sink.file(myFile), &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;print(tostring(myFile)) end &nbsp;

The variable myFile is not printable.  It’s an internal data structure that references the open file.   Put something like this after your http.request {} call

[lua]

    local contents = “”
    local file = io.open( path, “r” )
    if file then
        – read all contents of file into a string
        local contents = file:read( “*a” )
        io.close( file )
        print(contents)

    else

        print(“file not found”)

    end
[/lua]

That works, thanks a lot Rob!

You cannot write to system.ResourcesDirectory.  Try either system.CachesDirectory or system.DocumentsDirectory

Thanks for the quick answer but the problem seems to be somewhere else. I switched system.ResourcesDirectory to system.CachesDirectory and added a tostring() to the print but it still prints

file<closed>

function loadText() &nbsp;&nbsp;&nbsp;&nbsp;local path = system.pathForFile( "question.txt", system.CachesDirectory) &nbsp;&nbsp;&nbsp;&nbsp;local myFile = io.open( path, "w" )&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;http.request{&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;url = "http://filesmelt.com/downloader/question.txt", &nbsp;sink = ltn12.sink.file(myFile), &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;print(tostring(myFile)) end &nbsp;

The variable myFile is not printable.  It’s an internal data structure that references the open file.   Put something like this after your http.request {} call

[lua]

    local contents = “”
    local file = io.open( path, “r” )
    if file then
        – read all contents of file into a string
        local contents = file:read( “*a” )
        io.close( file )
        print(contents)

    else

        print(“file not found”)

    end
[/lua]

That works, thanks a lot Rob!

Hey if this works, can you please share the full working code

Hey if this works, can you please share the full working code