File io 在Lua中将文件参数传递给脚本
我需要读取一个我不知道名称的输入文件 我知道在C中我们可以做到这一点:File io 在Lua中将文件参数传递给脚本,file-io,lua,File Io,Lua,我需要读取一个我不知道名称的输入文件 我知道在C中我们可以做到这一点: 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) } 然后调用“/cprogram.txt.txt” 我可以用.Lua脚本执行类似操作
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)
}
然后调用“/cprogram.txt.txt
”
我可以用.Lua脚本执行类似操作吗?可以。从:
在开始运行脚本之前,lua
在名为arg
的全局表中收集命令行中的所有参数。脚本名称存储在索引0中,脚本名称后的第一个参数将转到索引1,依此类推
例如,您可以执行以下操作:
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
如果#arg<2,则
打印(“用法:lua“.arg[0]”)
返回
结束
本地ifile=io.open(参数[1],“r”)
本地文件=io.open(参数[2],“w”)
如果不是ifile或不是ofile,则
打印(“错误:无法打开文件”)
返回
结束
Lua脚本的命令行参数可用作arg[1]
,arg[2]
,。。。