Reputation: 25
I need to read a input file which name I do not know.
I know that in C we can do this:
FILE *Ifile;
File *Ofile;
int main(int argc, char *argv[]){
// Input and Output files
Ifile = fopen(argv[1],"r");
Ofile = fopen(argv[2],"w");
(More code)
}
and then call "./cprogram <any file name>.txt <any file name>.txt
"
Can I do something like this with .Lua scripts?
Upvotes: 1
Views: 1897
Reputation:
Yes, you can. From the documentation:
Before starting to run the script,
lua
collects all arguments in the command line in a global table calledarg
. The script name is stored at index 0, the first argument after the script name goes to index 1, and so on.
For example, you could do the following:
if #arg < 2 then
print ("usage: lua " .. arg[0] .. " <ifile> <ofile>")
return
end
local ifile = io.open(arg[1], "r")
local ofile = io.open(arg[2], "w")
if not ifile or not ofile then
print ("Error: could not open files")
return
end
Upvotes: 4