Using text of txt file

You didn’t give full specifics of what is in the file, however I think it may be easier to just use a text editor that has FIND & REPLACE functionality to prep the file for import as JSON.

If the file is formated like so:

“1. Why is the sky blue?”
“2. Why is grass green?”
“3. Why is water green or blue?”

You can easily make this into a file that can be JSON decoded.

If you add [at the beginning of the file,] and the end of the file and put a , between each question, it can be json decoded into a table. I would just insert the two brackets manually and then do a FIND & REPLACE for the carriage return, replace it with a carriage return with a , in front of it. Then just do something like the following in corona to put it in a table. To me this seems easier than coding something as most text editors can do the brunt of the work.

json = require("json")  
  
local text = {}  
  
local path = system.pathForFile( "questions.txt", system.ResourceDirectory )  
local fh = io.open( path, "r" )  
local data = fh:read( "\*a" )  
text = json.decode(data)  
  
print (text[1]) -- prints: 1. Why is the sky blue?  
print (text[2]) -- prints: 2. Why is grass green?  
print (text[3]) -- prints: 3. Why is water green or blue?  

From your description though I am not 100% sure how the file is formated. As other’s mentioned there could be answers in the file as well. Perhaps give as an example of what is really in the file? [import]uid: 56820 topic_id: 33696 reply_id: 134082[/import]

HI Brent and Anderoth

Thanks a lot for your comments will try both the ways you suggested and will let you know was i able to make ip with the work or not. But your answers were really impressive and it will really help me in future projects as well.

Once again thanks a lot man.

Regards
Varun [import]uid: 130269 topic_id: 33696 reply_id: 134115[/import]

– Hey varun, Just paste this text to main.lua and execute it. It’s working fine on my simulator
display.setStatusBar( display.HiddenStatusBar )
local function main()
-------------------- TO WRITE ANY FILE IN TEXT FORMAT ------------------------------------------
local path = system.pathForFile( “nick_name.txt”, system.DocumentsDirectory )
local file = io.open( path, “w+b” )
– save current state variables in comma separated list
file:write( “MY NAME IS HAROON " …”, “…” AND I LOVE"…", "…"CORONA, ")
– at the end, the comma and space is much more important. it is used to differentiate in different objects written in text file.
– for line in file:lines() do print(“line”) end
io.close( file )
----------------------TO READ ANY FILE ----------------------------------------------------------------
function explode(div,str)
if (div==’’) then
return false
end
local pos=0
local arr = {}
– for each divider found
for st,sp in function() return string.find(str,div,pos,true) end do
table.insert(arr,string.sub(str,pos,st-1)) – Attach chars left of current divider
pos = sp + 1 – Jump past current divider
end
table.insert(arr,string.sub(str,pos)) – Attach chars right of last divider
return arr
end

function nick_name( )

local path = system.pathForFile( “nick_name.txt”, system.DocumentsDirectory )
local file = io.open( path, “r” )

if file then
print(“loading nick_name”)
local contents = file:read( “*a” )
print("the contents of file is “…contents)
local prevState1 = explode(”, ", contents)
io.close( file )
return prevState1
end
end

local nick_name_data = {}
nick_name_data = nick_name( )
– the actual data in text file can be retrieve like this, nick_name_data[1] = MY NAME IS HAROON and nick_name_data[2] = AND I LOVE and in third nick_name_data[3] = CORONA
print(nick_name_data[1]…" its the first index “…nick_name_data[2]…” it is second index “…nick_name_data[3]…” it s third index")

return true
end

main() [import]uid: 108129 topic_id: 33696 reply_id: 134119[/import]

Anderoth, your method is much more easy and convenient to implement. Quick and easy approach.

Regards,
Haroon Yaseen [import]uid: 108129 topic_id: 33696 reply_id: 134121[/import]

Hi Haroon

I implemented the same code and now it is working fine, thanks a lot man.

Regards
Varun [import]uid: 130269 topic_id: 33696 reply_id: 134135[/import]

cancelled post

This is why we have sqlite.

cancelled post

This is why we have sqlite.