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

[error]please help with my file function

Started by wilcomega, 14 August 2012 - 09:16 AM
wilcomega #1
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
makerimages #2
Posted 14 August 2012 - 11:21 AM
you have a : instead of a .
wilcomega #3
Posted 14 August 2012 - 11:24 AM
i tried both f.readLine( ) and f:readLine( ) same error
makerimages #4
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
wilcomega #5
Posted 14 August 2012 - 11:28 AM
what is the f?
the file object :P/>/>


BTW: this section is called ask a PRO
makerimages #6
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
wilcomega #7
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
makerimages #8
Posted 14 August 2012 - 11:33 AM
sorry, no idea then
wilcomega #9
Posted 14 August 2012 - 12:23 PM
BUMP :P/>/>
BigSHinyToys #10
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
wilcomega #11
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
wilcomega #12
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?
Cranium #13
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.()
BigSHinyToys #14
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
Lyqyd #15
Posted 14 August 2012 - 03:08 PM
Make sure the file exists and that the fs.open() isn't returning nil and an error.
wilcomega #16
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/>/>
BigSHinyToys #17
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