453 posts
Location
Holland
Posted 14 August 2012 - 11:16 AM
the error is
main:65: attempt to index ? ( a nil value )
for you guys it is line 6, here is the code:
function fileToTable( sFile )
local f = fs.open( sFile, "r" )
local t = { }
local lastLine = ""
repeat
lastLine = f:readLine( )
if lastLine == nil or lastLine == "" then t[#t + 1] = lastLine end
until lastLine == nil or lastLine == ""
f.close( )
return t
end
thx in advance,
wilcomega
236 posts
Posted 14 August 2012 - 11:21 AM
you have a : instead of a .
453 posts
Location
Holland
Posted 14 August 2012 - 11:24 AM
i tried both f.readLine( ) and f:readLine( ) same error
236 posts
Posted 14 August 2012 - 11:27 AM
what is the f?
you could try f_readLine()
and also , why (dont think that matters, but) do you have ( ) instead of ()?
Edited on 14 August 2012 - 09:29 AM
453 posts
Location
Holland
Posted 14 August 2012 - 11:28 AM
what is the f?
the file object :P/>/>
BTW: this section is called ask a PRO
236 posts
Posted 14 August 2012 - 11:30 AM
Ask A Pro. (Anyone is able to answer your question, though "Pro's" will be designated by a special label.
is written.
but maybe f_readLine() might work
453 posts
Location
Holland
Posted 14 August 2012 - 11:32 AM
f_readLine( ) would be a serperate function
when i do f.readLine( ) i call a function inside the file object
just a note
236 posts
Posted 14 August 2012 - 11:33 AM
sorry, no idea then
453 posts
Location
Holland
Posted 14 August 2012 - 12:23 PM
BUMP :P/>/>
992 posts
Posted 14 August 2012 - 12:38 PM
Try this
Spoiler
function fileToTable( sFile )
local f = fs.open( sFile, "r" )
local t = { }
local lastLine
repeat
lastLine = f:readLine( )
table.insert(t,lastLine) -- that if ment no data when into the table
until lastLine == nil -- testing for "" meant any file that had blanck lines would stop ir reading there.
f.close( )
return t
end
local tFile = fileToTable("rom/user/BENCH4.lua") -- put location of your file in here
for i = 1,#tFile do
print(tFile[i])
end
453 posts
Location
Holland
Posted 14 August 2012 - 12:40 PM
Try this
Spoiler
function fileToTable( sFile )
local f = fs.open( sFile, "r" )
local t = { }
local lastLine
repeat
lastLine = f:readLine( )
table.insert(t,lastLine) -- that if ment no data when into the table
until lastLine == nil -- testing for "" meant any file that had blanck lines would stop ir reading there.
f.close( )
return t
end
local tFile = fileToTable("rom/user/BENCH4.lua") -- put location of your file in here
for i = 1,#tFile do
print(tFile[i])
end
doest work. same error while reading the file
453 posts
Location
Holland
Posted 14 August 2012 - 12:46 PM
you know i am always having problems with files in computercraft. btw do you know the IO version of this code?
3790 posts
Location
Lincoln, Nebraska
Posted 14 August 2012 - 01:37 PM
When declaring sFile, calling back to your function, are you using a string or a table? If it's a table, you may want to use textutils.serialize() to save it, and when reading, use textutils.unserialize.()
992 posts
Posted 14 August 2012 - 01:45 PM
here is io ver
Spoiler
function fileToTable( sFile )
if fs.exists(sFile) then
local file = io.open( sFile, "r" )
local t = { }
local lastLine
repeat
lastLine = file:read()
table.insert(t,lastLine) -- that if ment no data when into the table
until lastLine == nil -- testing for "" meant any file that had blanck lines would stop ir reading there.
file:close()
return t
else
return false
end
end
local tFile = fileToTable("rom/user/BENCH4.lua") -- put location of your file in here
for i = 1,#tFile do
print(tFile[i])
end
8543 posts
Posted 14 August 2012 - 03:08 PM
Make sure the file exists and that the fs.open() isn't returning nil and an error.
453 posts
Location
Holland
Posted 14 August 2012 - 03:15 PM
here is io ver
Spoiler
function fileToTable( sFile ) if fs.exists(sFile) then local file = io.open( sFile, "r" ) local t = { } local lastLine repeat lastLine = file:read() table.insert(t,lastLine) -- that if ment no data when into the table until lastLine == nil -- testing for "" meant any file that had blanck lines would stop ir reading there. file:close() return t else return false end end local tFile = fileToTable("rom/user/BENCH4.lua") -- put location of your file in here for i = 1,#tFile do print(tFile[i]) end
thx for some reason io works better i guess? anyways thx for helping this part was really important :P/>/>
992 posts
Posted 14 August 2012 - 03:18 PM
thx for some reason io works better i guess? anyways thx for helping this part was really important :P/>/>
and thank you for the +1