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

[Resolved]Reading file not working

Started by Ilikemlp123, 12 October 2017 - 01:11 AM
Ilikemlp123 #1
Posted 12 October 2017 - 03:11 AM
i'm trying to load a file for my script program (will be shown in media once complete), but whenever i try to run it with a file, it just gives an error on line 10 or 11
pastebin: uwvtyEhR

local tArgs = { ... } --args
if #tArgs == 0 then --args
print('Usage: lsc <path>') --Print Usage
else
local path = tArgs[1] --path
local Scr = fs.open('path','r') --attempted fs access
local loop = true --loop
while loop do --loop begin
local commands = {} --empties commands
local line = Scr.readLine(1) --reads line
if line then --if the line has things
text = nil --nillifies text
for text in line:gmatch('%w+') --attempts to split text
  do table.insert(commands,text) --more split
end--end split
for i=1,#commands do shell.run(commands[i]) --executes
end --end
else --else loop
loop = false --ends loop
end
end
end
i am a noob at lua.
pls help

Update: the bug was because i put quotes around a variable name
All fixed
lock the thread
Edited on 12 October 2017 - 08:19 PM
KingofGamesYami #2
Posted 12 October 2017 - 03:39 AM
I would hazard a guess that the file 'path' doesn't exist. Perhaps you meant to pass the contents of the variable path, which you defined on the previous line?
Ilikemlp123 #3
Posted 12 October 2017 - 03:46 AM
ok, i have a fix that might work. i'm adding a default for if the file doesn't exist
Dave-ee Jones #4
Posted 12 October 2017 - 04:26 AM
How are you calling the program? Like this?

lsc /dir/file --# /dir/file being the path to the file

You could try doing something like this so that there's a default (as you said):

local tArgs = { ... } or { "/dir/file" }
--# Alternatively..
local tArgs = { ... }
if #tArgs == 0 then
  tArgs[1] == "/dir/file"
end
--# Continue with your code..