Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How can I write/read multiple lines in a file? — Gideros Forum

How can I write/read multiple lines in a file?

AxlFlameAxlFlame Member
edited February 2013 in Game & application design
Hi guys, i'm having some issues regarding reading and writing data in a external file.
I wanna be able to save multiple highscores in a file and read them when it is needed, is that possible? Like, assume I have four minigames, when I play minigame #1 I want to save and read its score on line #1 of the save.txt file and so on.

This is what I have so far:
function GCendingScreen:loadHighscore()
	local file = io.open("|D|data.txt", "r");
	local score = 0;
	if file then
		score = tonumber(file:read());
		io.close(file);
	end
	return score;
end
 
function GCendingScreen:saveHighscore(savedScore)
	local file = io.open("|D|data.txt", "w+");
	if file then
		file:write(savedScore);
		io.close(file);
	end
end
The problem here is that I save and read only one highscore. Not only that but the saveHighscore function overwrite my previous data.txt file... Is there a way to specify it?

Comments

  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    Why won't you simply read and write table with values as scores, you could even use dataSaver:
    http://appcodingeasy.com/Gideros-Mobile/Save-and-load-data-module-for-Gideros-Mobile

    To not overwrite information you need to open file with append option as
    local file = io.open("|D|data.txt", "a");
    And you can also read by lines as specified here:
    http://www.lua.org/pil/21.1.html
    file:read("*line")
    and writing you'd need to append new line symbol
    file:write("your info \n")

    Likes: thanhquan1512

    +1 -1 (+1 / -0 )Share on Facebook
  • great job @ar2rsawseen, you are newbie's god, first answer for almost all questions :D

    thank you for your contribution :)

    Likes: ar2rsawseen

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.