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

[LUA] Help with reading fs files.

Started by BabyCodder, 02 April 2013 - 03:28 AM
BabyCodder #1
Posted 02 April 2013 - 05:28 AM
I need the following program to read all the data and print it: Im not sure why its not working.
Spoiler

function comR()
com = read()
navigate()
end

function ask()
term.clear()
term.setCursorPos(1,1)
print("Type 'add' to add a note.")
print("Type 'view' to view notes.")
print("Type 'delete' to delete notes.")
print("Type 'exit' to exit the program")
comR()
end
function navigate()
if com == "exit" then
  os.reboot()
elseif com == "add" then
  add()
elseif com == "view" then
  view()
elseif com == "delete" then
  del()
else
  print("Unknown input")
  sleep(2)
  ask()
end
end
function add()
local note = fs.open("myNotes/notes", "w")

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

print("Type your note.")
print("Or type 'exit' to reboot notes")
input = read()

if input == "exit" then
  ask()
else
note.writeLine( input )
ask()
note.close()
end
end
function view()
note = fs.open("myNotes/notes", "r")
print("")
print("notes:")
print(note.readAll())
note.close()
print("Please Re-Type Command")
comR()
end
ask()
It wont write more than one note and once you do write more than one the view does not return any notes.
D3add3d #2
Posted 02 April 2013 - 05:31 AM
Edit2: Try doing print(line) in FOR loop ending when line equals nil

Edit: derp… -_-/>
BabyCodder #3
Posted 02 April 2013 - 05:33 AM
Edit: derp… -_-/>

Umm….
SuicidalSTDz #4
Posted 02 April 2013 - 05:38 AM
Fs Library:
local handle = fs.open(yourFileName,"r")
local lines = handle.readLine()
for lines in handle.readLine do
 print(lines)
end
handle.close()

IO Library:
local handle = io.open("test","r")
local lines = handle:lines()
for lines in handle:lines() do
 print(lines)
end
handle:close()

EDIT: Remember to close your file handles :)/>
BabyCodder #5
Posted 02 April 2013 - 05:41 AM
Fs Library:
local handle = fs.open(yourFileName,"r")
local lines = handle.readLine()
for lines in handle.readLine do
print(lines)
end

IO Library:
local handle = io.open("test","r")
local lines = handle:lines()
for lines in handle:lines() do
print(lines)
end
thank you
SuicidalSTDz #6
Posted 02 April 2013 - 05:42 AM
It is my pleasure ^_^/>
BabyCodder #7
Posted 02 April 2013 - 06:11 AM
Fs Library:
local handle = fs.open(yourFileName,"r")
local lines = handle.readLine()
for lines in handle.readLine do
print(lines)
end
handle.close()

IO Library:
local handle = io.open("test","r")
local lines = handle:lines()
for lines in handle:lines() do
print(lines)
end
handle:close()

EDIT: Remember to close your file handles :)/>

The view does not work. it does not print anything however just looks for another input.
SuicidalSTDz #8
Posted 02 April 2013 - 06:22 AM
The what? Both of these script should work (You have to change the fileName of course)

EDIT: Just tested, these scripts both work flawlessly. Check the file name and make sure it exists on your computer.
BabyCodder #9
Posted 02 April 2013 - 06:33 AM
It gives me a error on line 55 of the following code:

Spoiler

function comR()
com = read()
navigate()
end

function ask()
term.clear()
term.setCursorPos(1,1)
print("Type 'add' to add a note.")
print("Type 'view' to view notes.")
print("Type 'delete' to delete notes.")
print("Type 'exit' to exit the program")
comR()
end
function navigate()
if com == "exit" then
  os.reboot()
elseif com == "add" then
  add()
elseif com == "view" then
  view()
elseif com == "delete" then
  del()
else
  print("Unknown input")
  sleep(2)
  ask()
end
end
function add()
local note = fs.open("myNotes/notes", "w")

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

print("Type your note.")
print("Or type 'exit' to reboot notes")
input = read()

if input == "exit" then
  ask()
else
note.writeLine( input )
ask()
note.close()
end
end
function view()
handle = fs.open("myNotes/notes", "r")
lines = handle.readLine()
for lines in handle.readLine do
  print("")
  print("notes:")
  print(lines)
  handle.close()
  print("Please Re-Type Command To Continue")
end
comR()
end
ask()

It's the "for handle in handle.readLine do"
SuicidalSTDz #10
Posted 02 April 2013 - 06:40 AM
You are closing the file handle inside the for loop that uses the file contents:


handle = fs.open("myNotes/notes", "r")
lines = handle.readLine()
for lines in handle.readLine do
  print("")
  print("notes:")
  print(lines)
  handle.close()
  print("Please Re-Type Command To Continue")
end

Close it afterward


handle = fs.open("myNotes/notes", "r")
lines = handle.readLine()
for lines in handle.readLine do
  print("")
  print("notes:")
  print(lines)
  print("Please Re-Type Command To Continue")
end
handle.close()