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

Need help on opening txt file

Started by okok99haha, 01 July 2014 - 12:16 PM
okok99haha #1
Posted 01 July 2014 - 02:16 PM
So, I was making a program that included editing .txt file.
It was doing well on opening the file, editing, and saving and etc.
However when it gives me the error on the line 15 of "attempt to call nil"
I have tried all of the things but does not work.
What is wrong with it? I just did the same thing that I did to other parts of the code that included opening the file.

local diskDriveSide = "left"

function RandomString()
  length = 16
  if length < 1 then return nil end
  local array = {}
  for i=1, length do
    array[i] = string.char(math.random(32,126))
end
  return table.concat(array)
end

function compare()
  local disk_f = io.open("disk/pw.txt", "r")
  local disk_pw = disk_f:write("*l")
  disk_fi:close()
  local lastID_f = io.open("lastid.txt", "r")
  local lastID = lastID_f:read("*l")
  lastID_f:close()
  while false do
    for fileNumber = 1, lastID do
	  local check_f = io.open(fileNumber.. ".txt","r")
	  local line = check_f:read("*l")
    if line == disk_pw then
	  local nick = check_f:read("*l")
	  print(nick)
	  return true
    else
	  return false
    end
  end
end
end

function newID()
  local genCode = RandomString()
  if disk.isPresent(diskDriveSide) == false then
    print("Insert Disk!")
    sleep(3)
   else
    term.setCursorPos(1,4)
    term.write("Please type in the player name : ")
    local playerName = read()
    disk.setLabel(diskDriveSide, "IDCard - " ..playerName)
    local disk_pw = io.open("disk/pw.txt", "w")
    disk_pw:write(genCode)
    disk_pw:close()
    local root_lastID = io.open("lastid.txt","r")
    local lastID = root_lastID:read("*l")
    root_lastID:close()
    root_lastID = io.open("lastid.txt","w")
    newID = lastID+1
    root_lastID:write(newID)
    root_lastID:close()
    local root_newID = io.open(newID..".txt","w")
    root_newID:write(genCode)
    root_newID:write("\n")
    root_newID:write(playerName)
    root_newID:close()
  end
    term.setCursorPos(1,6)
    print("Generated!")
    sleep(2)
end

function mainMenu()
  term.clear()
  term.setCursorPos(1,1)
  print("1. Verify")
  print("2. Make New ID Card")
  local event, key = os.pullEvent("char")
  if key == "1" then
    compare()
   elseif key == "2" then
	 newID()
   else
	 sleep(0.1)
end
end

while true do
  mainMenu()
end
sjonky #2
Posted 01 July 2014 - 03:30 PM
If i am counting correctly line 15 is the "disk_pw = disk_f…." and since disk_f is your file handle im guessing it fails beacause it cant find the file, or something. You should make it check first if the file is actually there. And check if the file handle creation was successful
Link149 #3
Posted 01 July 2014 - 03:45 PM
As sjonky said, it's probably because the program failed to open the file.
An easy way to check if a file has been openned correctly is to make sure it's handle isn't nil:


function read(sFilename)
  local hFile = io.open(sFilename, "r")
 
  #If file has been created then return it's content, otherwise
  #return nil (implicit)
  if hFile then
    local sBuffer = hFile:read("*a")
    hFile:close()
   
    return sBuffer
  end
end
Bomb Bloke #4
Posted 01 July 2014 - 03:46 PM
What's that "write" call doing there on line 15…?
electrodude512 #5
Posted 01 July 2014 - 05:40 PM
Line 15 says "disk_fi:close()" when you meant "disk_f:close()". Get rid of the i in disk_fi and it should work.
okok99haha #6
Posted 02 July 2014 - 01:12 AM
If i am counting correctly line 15 is the "disk_pw = disk_f…." and since disk_f is your file handle im guessing it fails beacause it cant find the file, or something. You should make it check first if the file is actually there. And check if the file handle creation was successful

Thanks, I will try.

As sjonky said, it's probably because the program failed to open the file.
An easy way to check if a file has been openned correctly is to make sure it's handle isn't nil:


function read(sFilename)
  local hFile = io.open(sFilename, "r")

  #If file has been created then return it's content, otherwise
  #return nil (implicit)
  if hFile then
	local sBuffer = hFile:read("*a")
	hFile:close()
  
	return sBuffer
  end
end

Wonder why it fails to open the file.
I will check when I get back home.

What's that "write" call doing there on line 15…?

Yeah was testing something and it was supposed to be line.
Sorry, mistake when uploading the code. Thanks.

Line 15 says "disk_fi:close()" when you meant "disk_f:close()". Get rid of the i in disk_fi and it should work.

Some typos to fix :(/>
Thanks for noticing that
okok99haha #7
Posted 02 July 2014 - 09:12 AM
Thank you everyone, solved the problem! :)/>