我想将一个表写入一个文件,该文件以其创建的日期和时间命名.我可以打开一个带有硬编码名称的文件,将表格写入其中,如下所示:
FILENAME_EVENTS="Events.txt" -- filename in string
local fp=io.open(FILENAME_EVENTS, a) -- open a new file with the file name
io.output(FILENAME_EVENTS) -- redirect the io output to the file
-- write the table into the file
for i, e in ipairs(eventlist) do io.write(e.title, e.category, e.ds, e.de, e.td) end
Run Code Online (Sandbox Code Playgroud)
但是当我尝试:
FILENAME_EVENTS=os.date().."\.txt" -- filename in string with date
local fp=io.open(FILENAME_EVENTS, a) -- open a new file with the file name
io.output(FILENAME_EVENTS) -- redirect the io output to the file
-- write the table into the file
for i, e in ipairs(eventlist) do io.write(e.title, e.category, e.ds, e.de, e.td) end
Run Code Online (Sandbox Code Playgroud)
我得到一个错误错误的参数#1到'输出'(10/06/11 17:45:01.txt:无效的参数)堆栈追溯:[C]:在函数'输出'
为什么这个"10/06/11 17:45:01.txt"是一个无效的论点?由于它包含空格或'/'?还是其他任何原因?
顺便说一句,该平台是win7 Pro + Lua 5.1.4获胜
显然它既是bork /又:是bork.第一个可能是因为它被视为目录分隔符.这可以证明如下:
fn=os.date()..'.txt'
print(io.open(fn,'w')) -- returns invalid argument
fn=os.date():gsub(':','_')..'.txt'
print(io.open(fn,'w')) -- returns nil, no such file or directory
fn=os.date():gsub('[:/]','_')..'.txt'
print(io.open(fn,'w')) -- returns file(0x...), nil <-- Works
Run Code Online (Sandbox Code Playgroud)
顺便说一句,你也可以考虑使用类似的东西,而不是使用奇怪的gsub和连接技巧
fn=os.date('%d_%m_%y %H_%M.txt')
Run Code Online (Sandbox Code Playgroud)