如何加载目录中的所有文件? [英] How to load all files from a directory?
问题描述
编辑:
对于windows,我很乐意看到一些真正的工作代码,尤其是对于lua。我可以使用boost :: filesystem for c ++。
对于Lua,您希望模块 Lua文件系统。正如Nick所观察到的那样,访问文件系统本身(而不是单个文件)不在C和C ++标准的范围之内。由于Lua本身是(用C语言实现require()的动态加载器的例外),所以核心语言缺少很多文件系统特性。
但是,由于(几乎)具有文件系统的任何平台也支持DLL或共享库,所以很容易扩展Lua核心。 Lua文件系统是一个可移植的库,它增加了对目录迭代,文件属性发现等的支持。
使用lfs,在Lua中模拟DIR的一些功能是基本上就像这样简单:
需要lfs
dot = arg [1]或者。
在lfs.dir(dot)中的名称做
本地fqn =点../..名称
本地attr = lfs.attributes(fqn)
print(name ,attr.mode,os.date(%Y-%m-%d%H:%M,attr.modification),attr.size)
end
产生如下输出:
E :...> t-lfs.lua
。目录2009-04-02 13:23 0
..目录2009-04-02 13:18 0
foo.txt文件2009-02-23 01:56 0
t-lfs .lua file 2009-04-02 13:18 241
E:...>
如果您的Lua副本来自 Lua for Windows ,那么你已经安装了lfs,上面的例子就可以使用了。顺便说一句,Lua解决方案也可能是一个明智的C或C ++解决方案。 Lua核心并不大,提供了一种动态的,垃圾收集的语言,并且可以很容易地与C进行交互,既可以作为托管应用程序,也可以作为扩展模块。要从C应用程序使用lfs,你需要链接到Lua DLL,初始化一个Lua状态,并通过<$ c来获得执行
requirelfs
的状态$ c> luaL_dostring()或者使用C API从全局表中检索require()
函数,将字符串lfs
,然后用类似于lua_pcall(L,1,1,0)
的方式调用Lua函数, code> lfs 在Lua堆栈顶部的表。
如果您已经有需要,这种方法可能是最有意义的对于嵌入式脚本语言,Lua符合您的要求。
Like the title says; how do I load every file in a directory? I'm interested in both c++ and lua.
Edit: For windows I'd be glad for some real working code and especially for lua. I can do with boost::filesystem for c++.
解决方案For Lua, you want the module Lua Filesystem.
As observed by Nick, accessing the file system itself (as opposed to individual files) is outside the scope of the C and C++ standards. Since Lua itself is (with the exception of the dynamic loader used to implement require() for C modules) written in standard C, the core language lacks many file system features.
However, it is easy to extend the Lua core since (nearly) any platform that has a file system also supports DLLs or shared libraries. Lua File system is a portable library that adds support for directory iteration, file attribute discovery, and the like.
With lfs, emulating some of the capability of DIR in Lua is essentially as simple as:
require "lfs" dot = arg[1] or "." for name in lfs.dir(dot) do local fqn = dot.."/"..name local attr = lfs.attributes(fqn) print(name, attr.mode, os.date("%Y-%m-%d %H:%M",attr.modification), attr.size) end
Which produces output that looks like:
E:...>t-lfs.lua . directory 2009-04-02 13:23 0 .. directory 2009-04-02 13:18 0 foo.txt file 2009-02-23 01:56 0 t-lfs.lua file 2009-04-02 13:18 241 E:...>
If your copy of Lua came from Lua for Windows, then you already have lfs installed, and the above sample will work out of the box.
Edit: Incidentally, the Lua solution might also be a sensible C or C++ solution. The Lua core is not at all large, provides a dynamic, garbage-collected language, and is easy to interact with from C either as a hosting application or as an extension module. To use lfs from a C application, you would link with the Lua DLL, initialize a Lua state, and get the state to execute the
require"lfs"
either vialuaL_dostring()
or by using the C API to retrieve therequire()
function from the global table, push the string"lfs"
, and call the Lua function with something likelua_pcall(L,1,1,0)
, which leaves thelfs
table on the top of the Lua stack.This approach probably makes the most sense if you already had a need for an embedded scripting language, and Lua meets your requirements.
这篇关于如何加载目录中的所有文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!