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

fsTest:13: attempt to index ? (a nil value)

Started by GodlyPlexi, 05 November 2014 - 05:20 PM
GodlyPlexi #1
Posted 05 November 2014 - 06:20 PM
I'm having some trouble with some code that I'm writing for my benefit to learn how to use the fs api a little better. Right now, what the program is trying to accomplish is to look at a file and store what's on it in a variable that I can print. Right now, I'm having some trouble with the fs.readLine() function not wanting to go through, and I can't seem to see why. I've looked at the common errors and such, but cannot seem to find why the function doesn't want to go through. So, please, help me if anyone can.


rAccessNum = {}
accessNum = fs.open("/common/test/accessNumb", "w")
accNumData = fs.open("/common/test/accessNum", "r")

if fs.exists("/common/test/") == false then
 fs.makeDir("/common/test/")
end

if fs.exists("/common/test/accessNumb") == false then
  accessNum()
  accessNum.close()
end

line = accNumData.readLine()

repeat
table.insert(rAccessNum, line)
line = accNumData.readLine()
until line == nil
accNumData.close()
realAccNum = rAccessNum[1]

if realAccNum == nil then
realAccNum = 0
end

realAccNum = realAccNum + 1
print("You've opened this file "..realAccNum.." times!")
accessNum.write(realAccNum)
accessNum.close()
KingofGamesYami #2
Posted 05 November 2014 - 06:43 PM
you define accessNum as the return of fs.open. I assume you wanted a function, eg:

accessNum = function() return fs.open( "/common/test/accessNumb" ) end

…although I'm not sure why you'd want to open then close a file…
GodlyPlexi #3
Posted 05 November 2014 - 07:41 PM
you define accessNum as the return of fs.open. I assume you wanted a function, eg:

accessNum = function() return fs.open( "/common/test/accessNumb" ) end

…although I'm not sure why you'd want to open then close a file…
I was trying to make it open and close it to make sure it exists if it didn't, mostly just to see if that could be done. At this point in time, I'm mostly messing with the fs api just to get an idea of how it works and how I can use it.

And also, I tried to define accessNum as the file handle, so I could use things like fs.write and fs.readLine. I'm not sure I wanted a funtction, and I may have messed up more than I thought… And even after editing that in, it still returns the error fsTest:14: attempt to index ? (a nil value) when I run the program, and I've made sure that I spelled it all right and the like, so I'm still not sure what's wrong. I'm sorry if this seems really obvious or anything, I'm just really clueless.
TheJebForge #4
Posted 05 November 2014 - 07:41 PM
Have you tried changing position of that line thing?
Try to cut it and paste right after opening that file! And close after that line thing!
Let me know if it works!
valithor #5
Posted 05 November 2014 - 09:24 PM
Just so you know accessNum and accNumbData is not using the same file for the read and write. Because of this, even though you do the error check right above where you try to set the line variable, the file that it is trying to find for the read still does not exist.

accessNum = fs.open("/common/test/accessNumb", "w")
accNumData = fs.open("/common/test/accessNum", "r")
Edited on 05 November 2014 - 08:25 PM
Bomb Bloke #6
Posted 05 November 2014 - 10:29 PM
And that's putting aside that opening the file in write mode wipes its contents, and you can't open a file in multiple modes at the same time.

Thus if you want to alter the first line of your file, you need to:

1) Read everything in the file, then close the handle.
2) Alter the first value you loaded into memory.
3) Re-write the whole file.

I'd script it up along these lines:

local filePath, rAccessNum, accessNum = "/common/test/accessNum", {}

-- Do our existence checks first (fs.getDir() requires CC1.6+):
if not fs.exists(fs.getDir(filePath)) then fs.makeDir(fs.getDir(filePath)) end

-- And if the file exists:
if fs.exists(filePath) then
	-- Now open for read access and attempt to gather data:
	accessNum = fs.open(filePath, "r")
	
	repeat
		local line = accessNum.readLine()
		table.insert(rAccessNum, line)
	until not line

	accessNum.close()

	-- File read operations return strings, so convert type:
	local realAccNum = tonumber(rAccessNum[1])

	if not realAccNum then realAccNum = 0 end
	realAccNum = realAccNum + 1
	print("You've opened this file "..realAccNum.." times!")
	rAccessNum[1] = tostring(realAccNum)
else  -- Or if the file doesn't exist:
	print("File didn't exist! Opening for the first time!
	rAccessNum[1] = "1"
end

-- Either way, write up a new version:
accessNum = fs.open(filePath, "w")
for i=1,#rAccessNum do accessNum.writeLine(rAccessNum[i]) end
accessNum.close()
Edited on 07 November 2014 - 05:27 AM
GodlyPlexi #7
Posted 07 November 2014 - 06:24 AM
And that's putting aside that opening the file in write mode wipes its contents, and you can't open a file in multiple modes at the same time.

Thus if you want to alter the first line of your file, you need to:

1) Read everything in the file, then close the handle.
2) Alter the first value you loaded into memory.
3) Re-write the whole file.

I'd script it up along these lines:

local filePath, rAccessNum, accessNum = "/common/test/accessNum", {}

-- Do our existence checks first (fs.getDir() requires CC1.6+):
if not fs.exists(fs.getDir(filePath)) then fs.makeDir(fs.getDir(filePath)) end

-- And if the file exists:
if fs.exists("/common/test/accessNumb") then
	-- Now open for read access and attempt to gather data:
	accessNum = fs.open(filePath, "r")
	
	repeat
		local line = accessNum.readLine()
		table.insert(rAccessNum, line)
	until not line

	accessNum.close()

	-- File read operations return strings, so convert type:
	local realAccNum = tonumber(rAccessNum[1])

	if not realAccNum then realAccNum = 0 end
	realAccNum = realAccNum + 1
	print("You've opened this file "..realAccNum.." times!")
	rAccessNum[1] = tostring(realAccNum)
else  -- Or if the file doesn't exist:
	print("File didn't exist! Opening for the first time!
	rAccessNum[1] = "1"
end

-- Either way, write up a new version:
accessNum = fs.open(filePath, "w")
for i=1,#rAccessNum do accessNum.writeLine(rAccessNum[i]) end
accessNum.close()
Ok, thank you! I think I understand this a bit more now, I really appreciate your help and patience with me. :)/>