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

Multiple Line Writing With File.write With For I = 1 , Data

Started by Kamefrede, 05 September 2013 - 11:46 AM
Kamefrede #1
Posted 05 September 2013 - 01:46 PM
My Problem is that i want to write multiple Turtle ID's or any Number really into a file using the for i = 1 , data but it only writes the last number :l
For anyone who wants to know the purpose of the code: CCpoly (Computercraft Monopoly
my code
Spoiler
  • print("How many players do you want?1 Turtle = 1 Player")
  • local input = tonumber(read())
  • local file = fs.open("monooptions","w")
  • file.write(textutils.serialize(input))
  • textutils.unserialize(input)
  • file.close()
  • print("Starting CCpolio")
  • sleep(3)
  • local bimp = fs.open("monooptions","r")
  • local data = bimp.readAll()
  • for i=1, data do
  • local durk = fs.open("monop","w")
  • print("Turtle Id")
  • local derp = tonumber(read())
  • durk.write(textutils.serialize(derp))
  • durk.close()
  • end
svdragster #2
Posted 05 September 2013 - 02:02 PM

file = fs.open("monop", "w")
for i=1, data do
  local int = tonumber(read())
  file.writeLine(int)
end
file.close()

The problem is that the mode w overwrites the content of the file. So you have to open the file and then write the stuff in there.
Kamefrede #3
Posted 05 September 2013 - 02:14 PM
Ty :D/>
Kamefrede #4
Posted 05 September 2013 - 02:21 PM
For some reason on monop file it writes anynumber.0 not anynumber anyway to fix that? :c
svdragster #5
Posted 05 September 2013 - 02:45 PM
try tostring(int)
Kamefrede #6
Posted 05 September 2013 - 02:58 PM
Worked Perfectly Thanks!
Lyqyd #7
Posted 05 September 2013 - 02:58 PM
Why on earth are you using textutils.serialize? That's not what it's for.