Jump to content
  • 0

Write/Read If True File


1x1

Question

Hello, so i wanna make a lua script that write a file and I'll use another lua script to read this file if is true or not.

 

So i will create a file like this

1 - False
2 - False
3 - False

So i'll ask Write.lua to change 1 into True. Then Read.lua Will check if 1 is true or not.

 

Same goes to 2 and 3.

Please help me :) 

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

I think putting the line number is a bad idea cause it require you to do extra parsing when you will read the data in it
 

local function appendFile(fileName, data)
  local file, err = io.open(fileName, "a+")
  file:write(tostring(data) .. "\n")
  file:close()
end

local function readLine(fileName, lineNumber)
  local data
  pcall(function()
    local f = io.lines(fileName)
    if lineNumber == 1 then
      data = f()
      return
    end
    while true do
      local line = f()
      if not line then break end
      lineNumber = lineNumber - 1
      if lineNumber == 0 then
        data = line
        break
      end
    end
  end)
  return data
end

-- write to files ( file name, data to write )
appendFile("test.txt", true)
appendFile("test.txt", true)
appendFile("test.txt", false)
appendFile("test.txt", true)

-- read from files ( file name, line numberic )
print(readLine("test.txt", 1))
print(readLine("test.txt", 2))
print(readLine("test.txt", 3))
print(readLine("test.txt", 4))

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.