Reading specific text line

Hey guys, I have recently been learning all about reading and writing text files. I have though of a great idea but I do not know how to apply it. I want my app to be able to read a specific line (that I tell it to). Is it possible to do something like “save line 1 as name, save line 2 as date, save line 3 as field1, save line 3 as field3, etc…” or like “save line 16 as a variable”.

Thanks for any and all help!

Regards,

Summit Tech

Hi Summit Tech,

Do you want to write a new text file in this fashion? Or do you want to “overwrite a particular line” in an existing text file, based on its line number? Can you show me an example of what this text file might look like, and what you want to accomplish with it? There are ways to do basically whatever you need, but I’ll need more details on what you need to do.

Regards,

Brent

Sure! So this is my text file layout.

Workout name Number of workouts Repetitions Breaks Between Activities Breaks Between Repetitions Stop Sounds Are the workouts in random order? Workout 1's name Time Workout 2's name Time Workout 3's name Time Workout 4's name Time Workout 5's name Time Workout 6's name Time Workout 7's name Time Workout 8's name Time Workout 9's name Time Workout 10's name Time Workout 11's name Time

I want a function that can set 1-7 with a unique name, but then I want the function to set the workouts and workout times equal to certain variables, like workout1, workout2 ,workout3… etc. The user can determined how many workouts he/she wants to have. Using the lines 1-7, a function will set up the workout’s settings. Then I will have another function read the workout1… lines and show them for the amount of time allotted (under the workout’s name.) 

I don’t need to write to a specific line, I just need to be able to read a line.

Thanks for all the help Brent!

Possibly make a table??? 

I have no clue!

Hi Summit Tech,

Although I can come up with a way to do this in a text file, perhaps a SQLite database or JSON would be better. In fact, JSON is nice because Corona can convert it to a Lua table (and vice-versa, Lua table to JSON) with the functions described here:

http://docs.coronalabs.com/api/library/json/index.html

Just food for thought… if you want to retain the text file use, I can suggest a way to handle it.

Brent

Thanks!

Will defiantly use a JSON file!

How would you call a certain line/value from the table.

And how would you allow the user the changes the number of workouts? (See my second post for more info)

Hey, so here is my code.

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- local json = require ("json") display.setStatusBar( display.HiddenStatusBar ) background = display.newRect(0,-90,640,1136) title = display.newText( "File Demo", 0, 30, "Verdana-Bold", 20 ) title.x = display.contentWidth/2 -- center title title:setTextColor( 255,0,0 ) t={ name="Max", age="14" } local json = require("json") function saveTable(t, filename) local path = system.pathForFile( filename, system.DocumentsDirectory) local file = io.open(path, "w") if file then local contents = json.encode(t) file:write( contents ) io.close( file ) title:setTextColor( 255,255,0 ) root1 = display.newText( path, 0, 300, "Verdana-Bold", 5 ) root1.x = display.contentWidth/2-- center title root1:setTextColor( 0,0,255 ) return true else return false end end function loadTable(filename) path = system.pathForFile( filename, system.DocumentsDirectory) contents = "" myTable = {} file = io.open( path, "r" ) if file then contents = file:read( "\*a" ) local myTable = json.decode(contents); print( "Contents of " .. path .. "\n" .. contents ) print ( "decoded new json" ) decoded = true io.close( file ) title:setTextColor( 255,0,255 ) myTable.name = name1 testText = display.newText( name1, 300, 300, "Verdana-Bold", 5 ) return myTable end return nil end loadTable("test.json")

My saveTable function was a success.

 On lines 56 and 57 you can see that I try to take a value from myTable and make it a string then try to print it, but it fails- saying “Line 57, bad argument, string expected, got nil”

Any help to this issue? 

Hi SummitTech,

I don’t see any declaration of “name1” except on line 56, not even an upvalue somewhere above and outside the function. This is why line 57 is probably failing… it doesn’t know what “name1” is.

Brent

So I added “local name1” to line 16.

Unfortunately the same error occurred.

Bad argument #1 to ‘newText’ (string expected, got nil)

Instead of JSON, you might try using VIVE - I’ve just released it, and I’d like for people to try it out :slight_smile:

www.gymbyl.com/programming/vive.html

It’s quite a bit more flexible than JSON, and uses no dependencies.

C

Thanks Caleb, I may have a look at it!

But without it, it still wont work. Is it even possible to set a single piece of information in a table to a string? I’m pretty sure, so why does my code now work?!

Are you not just switching around the variables in line 56?

myTable.name = name1

should be

name1 = myTable.name

Thank you jonjonsson!

My troubles are solve!

Regards.

Hi Summit Tech,

Do you want to write a new text file in this fashion? Or do you want to “overwrite a particular line” in an existing text file, based on its line number? Can you show me an example of what this text file might look like, and what you want to accomplish with it? There are ways to do basically whatever you need, but I’ll need more details on what you need to do.

Regards,

Brent

Sure! So this is my text file layout.

Workout name Number of workouts Repetitions Breaks Between Activities Breaks Between Repetitions Stop Sounds Are the workouts in random order? Workout 1's name Time Workout 2's name Time Workout 3's name Time Workout 4's name Time Workout 5's name Time Workout 6's name Time Workout 7's name Time Workout 8's name Time Workout 9's name Time Workout 10's name Time Workout 11's name Time

I want a function that can set 1-7 with a unique name, but then I want the function to set the workouts and workout times equal to certain variables, like workout1, workout2 ,workout3… etc. The user can determined how many workouts he/she wants to have. Using the lines 1-7, a function will set up the workout’s settings. Then I will have another function read the workout1… lines and show them for the amount of time allotted (under the workout’s name.) 

I don’t need to write to a specific line, I just need to be able to read a line.

Thanks for all the help Brent!

Possibly make a table??? 

I have no clue!

Hi Summit Tech,

Although I can come up with a way to do this in a text file, perhaps a SQLite database or JSON would be better. In fact, JSON is nice because Corona can convert it to a Lua table (and vice-versa, Lua table to JSON) with the functions described here:

http://docs.coronalabs.com/api/library/json/index.html

Just food for thought… if you want to retain the text file use, I can suggest a way to handle it.

Brent

Thanks!

Will defiantly use a JSON file!

How would you call a certain line/value from the table.

And how would you allow the user the changes the number of workouts? (See my second post for more info)

Hey, so here is my code.

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- local json = require ("json") display.setStatusBar( display.HiddenStatusBar ) background = display.newRect(0,-90,640,1136) title = display.newText( "File Demo", 0, 30, "Verdana-Bold", 20 ) title.x = display.contentWidth/2 -- center title title:setTextColor( 255,0,0 ) t={ name="Max", age="14" } local json = require("json") function saveTable(t, filename) local path = system.pathForFile( filename, system.DocumentsDirectory) local file = io.open(path, "w") if file then local contents = json.encode(t) file:write( contents ) io.close( file ) title:setTextColor( 255,255,0 ) root1 = display.newText( path, 0, 300, "Verdana-Bold", 5 ) root1.x = display.contentWidth/2-- center title root1:setTextColor( 0,0,255 ) return true else return false end end function loadTable(filename) path = system.pathForFile( filename, system.DocumentsDirectory) contents = "" myTable = {} file = io.open( path, "r" ) if file then contents = file:read( "\*a" ) local myTable = json.decode(contents); print( "Contents of " .. path .. "\n" .. contents ) print ( "decoded new json" ) decoded = true io.close( file ) title:setTextColor( 255,0,255 ) myTable.name = name1 testText = display.newText( name1, 300, 300, "Verdana-Bold", 5 ) return myTable end return nil end loadTable("test.json")

My saveTable function was a success.

 On lines 56 and 57 you can see that I try to take a value from myTable and make it a string then try to print it, but it fails- saying “Line 57, bad argument, string expected, got nil”

Any help to this issue? 

Hi SummitTech,

I don’t see any declaration of “name1” except on line 56, not even an upvalue somewhere above and outside the function. This is why line 57 is probably failing… it doesn’t know what “name1” is.

Brent