我想写一个表到一个文件中,这个文件以它创建的日期和时间命名。我可以打开一个带有硬编码名称的文件,将表写入其中,如下所示:
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但是当我尝试的时候:
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我收到错误'output‘(10/06/11 17:45:01.txt:无效参数)的错误参数#1堆栈回溯: C: in function 'output’
为什么这个"10/06/11 17:45:01.txt“是一个无效参数?由于它包含空格或'/'?或者其他原因?
顺便说一句,平台是win7 Pro + Lua 5.1.4 for win
发布于 2011-10-06 18:18:31
显然,/和:都是那个博克。第一个可能是因为它被认为是目录分隔符。这可以演示如下:
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顺便说一句,除了使用奇怪的gsub和连接技巧之外,您还可以考虑使用类似于
fn=os.date('%d_%m_%y %H_%M.txt')https://stackoverflow.com/questions/7672673
复制相似问题