我正在使用Lua脚本来确定文件大小:
local filesize=0
local filePath = "somepath.bin"
local file,msg = io.open(filePath, "r")
if file then
filesize=file:seek("end")
file:close()
filePresent = true
end
但是,这似乎仅适用于最大2GB的文件。对于较大的文件,
filesize
始终为nil
。 io.open
有任何限制吗?如果是这样,我该如何解决?在Windows Server 2008 R2 64bit上运行Lua 5.1.4
最佳答案
问题不在于io.open
,而在于file:seek
。您可以像这样检查错误:
filesize, err = file:seek("end")
if not filesize then
print(err)
end
错误消息可能是
Invalid argument
。这是因为对于大于2GB的文件,其大小超过了32位long
可以容纳的大小,这导致C函数fseek
无法正常工作。在POSIX系统中,Lua使用
fseeko
而不是off_t
中的long
来获取fseek
的大小。在Windows中,有一个_fseeki64
,我想它可以完成类似的工作。如果这些不可用,则使用fseek
,这将导致问题。相关来源是
liolib.c
(Lua 5.2)。正如@lhf指出的那样,在Lua 5.1中,始终使用fseek
(source)。升级到Lua 5.2可能会解决该问题。关于windows - Lua-io.open()最多2 GB?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26754703/