Jump to content




[Resolved]Reading file not working


3 replies to this topic

#1 Ilikemlp123

  • Members
  • 12 posts

Posted 12 October 2017 - 02: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 by Ilikemlp123, 12 October 2017 - 09:19 PM.


#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 12 October 2017 - 02: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?

#3 Ilikemlp123

  • Members
  • 12 posts

Posted 12 October 2017 - 02:46 AM

ok, i have a fix that might work. i'm adding a default for if the file doesn't exist

#4 Dave-ee Jones

  • Members
  • 456 posts
  • LocationVan Diemen's Land

Posted 12 October 2017 - 03: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..






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users