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

Read from file always returns only the first line 5 times. Please Help.

Started by mark332, 08 April 2013 - 04:08 AM
mark332 #1
Posted 08 April 2013 - 06:08 AM
Hello.

I decited to make an operating-system for my MindCrack Server and this is the installation program, but there's still one problem,
the program has to read the filenames for the OS from a file, insert them into a table and copy them, but if the file has 5 lines it always inserts the first line 5 times in the table.

Please help me, I've already tried everything I know about lua and nothing worked.


Code:

os.loadAPI("rom/BeTECH/API/gen")
gen.startOS()
gen.clear(1,1)
print("BeTECH---Be_Technology!")
print(" ")
print("BeTECH--OS -- Installer")
print(" ")
print("Sammle Informationen!")

local file3 = fs.open("rom/BeTECH/installer/destination","r")
destination = {}
local line3 = file3.readLine()
repeat
table.insert(destination, line3)
local line3 = file3.readLine()
until line3 == nil
file3.close()
sleep(1)
local file1 = fs.open("rom/BeTECH/installer/recurce","r")
recurce = {}
local line1 = file1.readLine()
repeat
table.insert(recurce, line1)
local line1 = file1.readLine()
until line1 == nil
file1.close()
sleep(1)
local file2 = fs.open("rom/BeTECH/installer/dirs","r")
dirs = {}
local line2 = file2.readLine()
repeat
table.insert(dirs, line2)
local line2 = file2.readLine()
until line2 == nil
file2.close()
--if you go on, it will always copy one file 5 times --
-- if I write this:
for i=0, 5 do
print(destination[i])
print(recurce[i])
print(dirs[i])
end
-- It tells me 5 times:
--
--BeTECH/BeTECH-OS
--rom/BeTECH/BeTECH-OS
--BeTECH

--------------------------------------------------------------
sleep(0,5)
print("Suche auf existierende Verzeichnisse!")
i = 1
repeat
if fs.exists(destination[i]) then
print("File :"..i.." found -- Deleting!")
fs.delete(destination[i])
end
sleep(0,4)
i = i + 1
until i == #destination + 1
print("Erstelle neue Verzeichnisse!")
i = 1
for i = 1, #dirs do
fs.makeDir(dirs[i])
print(dirs[i])
sleep(0,4)
end
print("Installiere!")
i = 1
for i = 1, #destination do
if not fs.exists(destination[i]) then
fs.copy(recurce[i], destination[i])
end
sleep(0,8)
end
print("Done")
sleep(2)
os.shutdown()



Here are the other 3 files (from where it has to read)


destination:

BeTECH/BeTECH-OS
startup
BeTECH/REG_HKEY
BeTECH/USERDATA
BeTECH/version

recurce:

rom/BeTECH/BeTECH-OS
rom/BeTECH/startup
rom/BeTECH/REG_HKEY/
rom/BeTECH/USERDATA
rom/BeTECH/main/version

And dirs:

BeTECH
BeTECH/REG_HKEY
BeTECH/USERDATA
BeTECH/USERDATA



I hope you can help me.

Greetings mark332

PS: Please Excuse misspellings, I'm from Germany and not the best in english :)/>

EDIT: I've already tried to rename/recreate the files, but it won't work.
Lyqyd #2
Posted 09 April 2013 - 04:52 AM
Split into new topic.

Inside the loops, take out the local keyword from the readLine lines. You're declaring the variable local to that iteration of the loop. It is then destroyed when the loop goes back to the beginning, so it uses the upvalue each time, which is the first line.
mark332 #3
Posted 10 April 2013 - 06:46 AM
I'll try it out.


Edit: The installer works now, but now I've got another problem. Thanks for your reply. :)/>

I'll post it, when I have more time.
Edited on 10 April 2013 - 05:37 AM