Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
TheJebForge

FileSystem problem

Question

I want to make my bank system for my server, but I got problem when making bank network.

 

When server tries write a file, it got an error like fil = nil. I can't solve the problem, so please help me!

There's the code:

local com = require("component")
local ev = require("event")
local fs = require("filesystem")
local net = com.modem
net.open(15)
print("Opened Net. Waiting for commands")
while true do
  local _,_,from,port,_,message,nick,balance = ev.pull("modem_message")
  if message == "getInfo" then
    if fs.exists("bankAccounts/"..nick) then
      local fil = io.open("bankAccounts/"..nick,"r")
      bal = fil:read(1000)
      fil:close()
      net.send(from,15,nick,bal)
    else
      net.send(from,15,"no info")
    end
  end
  if message == "setInfo" then
    local fil = io.open("bankAccounts/"..nick,"wb")
    fil:write(balance)
    fil:close()
    net.send(from,15,"done it")
  end
end
Link to post
Share on other sites

6 answers to this question

Recommended Posts

  • 0

What is if you change 'binary' write-mode to normal? -> "w" instead of "wb"

And does the directory 'bankAccounts/' exists?

Add something like

if not fs.isDir("bankAccounts") then
    fs.makeDir("bankAccounts")
end
before your loop..

And btw: instead of using ev.pull("modem_message") inside of a loop you should use event.listen("modem_message", <yourfunction>) but after that you still need something so script doesnt quit -> a loop ;)

E.g.:

if not fs.isDir("bankAccounts") then
  fs.makeDir("bankAccounts")
end

function messageReceived(event, localAddress, remoteAddress, port, senderDistance, message, nick, balance)
  nick = nick or nil
  balance = balance or nil
  if message == "getInfo" then
    ...
  elseif message == "setInfo" then
    local fil = io.open("bankAccounts/"..nick, "w")
    fil:write(balance)
    fil:close()
    net.send(remoteAddress, 15, "done it")
  end
end
event.listen("modem_message", messageReceived)

while true do
  os.sleep(1)
end
Link to post
Share on other sites
  • 0

bankAccounts folder exists. Just it can't make a filestream. Error is: error to get global fil (got a nil).

When I try to switch from wb to w. Nothing happens. Same error.

With events anything is okay. Error appears when computer got a message and tries to make changes to file system.

Link to post
Share on other sites
  • 0

Try attempting to find out where exactly fil is nil. Add the below to places where fil is used.

if fil == nil then
 print("Something's not right.")
end

Add the line number the if is at to the message printed, then run the program.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.