Hello Varun,
I use the same basic method as Haroon to read the text file into a Lua string named “contents”, like this:
local filePath = system.pathForFile( "your\_file.txt", system.DocumentsDirectory )
local contents = ""
local file = io.open( filePath, "r" )
if ( file ) then
contents = file:read( "\*a" ) ; io.close( file ) ; file = nil ; filePath = nil
end
But instead of using string.find, I use a nested series of string matching and group matching to search for “patterns” within the contents string.
Using group matching (string.gmatch), you would find a “pattern” that accommodates the entire chunk of each question in your exam text file. For example, you might determine that each question “begins with a number, followed by a period, and ends with 2 newline breaks”.
Once you have a method of group matching to separate the entire text file into individual questions (each will be a new string), you loop through that part and string-match or group-match to make the question into clear “parts” that you can manipulate in Lua, for example"
– question number
– question text
– possible answers (multiple choice?)
– correct answer
Unfortunately, string matching is a very complicated and tedious procedure, because it needs to be EXACT to pull out exactly what you need. But, that’s the necessary method, unless you want to manually cut-and-paste every question from your text file into Lua table or SQLite database, or XML, JSON, etc.
Here’s the Lua documentation on the string matching functions.
http://docs.coronalabs.com/api/library/string/gmatch.html
http://docs.coronalabs.com/api/library/string/match.html
http://developer.coronalabs.com/content/strings (look under “Patterns”)
Since this is a very complex procedure, I might be able to help you with a sample string search if you provide me with one sample question from your exam. Post the entire question text as it appears in your text file, and I’ll take a look.
Brent [import]uid: 200026 topic_id: 33696 reply_id: 134044[/import]