This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
TyDoesMC's profile picture

how to make a program write to a file?

Started by TyDoesMC, 17 May 2014 - 01:38 AM
TyDoesMC #1
Posted 17 May 2014 - 03:38 AM
Im making a program take user input and save it to a disk or a file like a player data base add a name, and detials it turns it to a string like:

local player[1] = "Example"
local playerDetail[1] = "Example"
print(allplayersifpossible)
allplayersifpossible is another thing i need help with. how do I make it print all the player[1-999999]
and playerDetail[1-99999].
any help is appreciated :D/>
KingofGamesYami #2
Posted 17 May 2014 - 04:14 AM
for loops are amazing for something this simple.

for i = 1, #player do --#this assumes your table is numerically indexed, eg. {"example", "another", "hello"} and that both tables will have the same number for player and playerDetail.
 print(player[i]..' '..playerDetail[i]) --# notice that this code would print "Example Example" if used on the tables you defined above (or rather didn't define)
end
Remember, you have to create a table before assigning any data through keys, and you cannot use local before it

local player = {}
player[1] = "Example"
local playerDetail = {}
player[1] = "Example"
--#you can also do this
local player = {"Example", "Example"}
local playerDetail = {"Example", "Example"}
The way I would handle what you are doing:

local players = {}
players["playerName"] = 'bla bla bla details'
--# so I could do this
for k, v in pairs(players) do
 print(k..":"..v)
end
--# and to add a player
players["KingofGamesYami"] = "the best"
--# the result of using my loop on the table containing what I just defined
KingofGamesYami:the best
Edited on 17 May 2014 - 02:19 AM
RoD #3
Posted 17 May 2014 - 03:43 PM
for writing to a file simply do:

file = fs.open("myFiles/log", "w") --#will open the file "log" and write to it whenever we want
file.write("Line One") --#This writes to the file the string "Line One". Note that you can use variables and table values
file.close()
Blue #4
Posted 17 May 2014 - 05:22 PM
for writing to a file simply do:

file = fs.open("myFiles/log", "w") --#will open the file "log" and write to it whenever we want
file.write("Line One") --#This writes to the file the string "Line One". Note that you can use variables and table values
file.close()
You can also use "writeLine" instead of "write".
Edited on 17 May 2014 - 03:22 PM
TyDoesMC #5
Posted 18 May 2014 - 12:42 AM
Thankyou all for your feedback, I was planning on making a Database thats editable but my understanding Im not that advanced
yet.
RoD #6
Posted 18 May 2014 - 12:46 AM
you really want to get into tables. That is usefull for "databases". Good luck
mistamadd001 #7
Posted 19 May 2014 - 01:59 AM
editable databases are easy with tables, simply call up the table and (using the example earlier) just say

players["mistamadd001"] = "old info"

--then to edit the table/db is recall that part of the table using
players["mistamadd001"] = "new info"

that will replace the old info with new info. if you use the for loop thing you can print the whole DB to screen simply by using the example above so if we combine both examples we would get


KingofGamesYami : the best
mistamadd001 : new info
playername : info

then to write the table to a file simply do a file.write()


local file = fs.open("directory/filename", "w")
file.write(players)
file.close()
--each time you wanna edit the db you pull it from the file like this
local file = fs.open("directory/filename", "r")
players = file.readAll()
file.close()