Like the title says; how do I load every file in a directory? I'm interested in both c++ and lua.
就像标题所说;如何加载目录中的每个文件?我对c ++和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++.
编辑:对于Windows我会很高兴一些真正的工作代码,尤其是对于lua。我可以用boost :: filesystem for c ++。
4 个解决方案
#1
For Lua, you want the module Lua Filesystem.
对于Lua,您需要模块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.
正如Nick所观察到的那样,访问文件系统本身(而不是单个文件)超出了C和C ++标准的范围。由于Lua本身(用于实现C模块的require()的动态加载器除外)用标准C编写,因此核心语言缺少许多文件系统功能。
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.
但是,很容易扩展Lua核心,因为(几乎)任何具有文件系统的平台也支持DLL或共享库。 Lua文件系统是一个可移植的库,它增加了对目录迭代,文件属性发现等的支持。
With lfs, emulating some of the capability of DIR in Lua is essentially as simple as:
使用lfs,在Lua中模拟DIR的一些功能基本上就像:
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.
如果你的Lua副本来自Lua for Windows,那么你已经安装了lfs,上面的示例将开箱即用。
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 via luaL_dostring()
or by using the C API to retrieve the require()
function from the global table, push the string "lfs"
, and call the Lua function with something like lua_pcall(L,1,1,0)
, which leaves the lfs
table on the top of the Lua stack.
编辑:顺便说一下,Lua解决方案也可能是一个合理的C或C ++解决方案。 Lua核心并不大,提供动态的垃圾收集语言,并且很容易与C作为托管应用程序或扩展模块进行交互。要使用来自C应用程序的lfs,您将链接Lua DLL,初始化Lua状态,并通过luaL_dostring()或使用C API检索require()函数来获取执行require“lfs”的状态从全局表中,推送字符串“lfs”,并使用类似lua_pcall(L,1,1,0)的函数调用Lua函数,这将lfs表保留在Lua堆栈的顶部。
This approach probably makes the most sense if you already had a need for an embedded scripting language, and Lua meets your requirements.
如果您已经需要嵌入式脚本语言,并且Lua满足您的要求,这种方法可能最有意义。
#2
For a C++ solution, have a look at the Boost.Filesystem library.
对于C ++解决方案,请查看Boost.Filesystem库。
#3
Listing files in a directory is defined by the platform so you would have to use a platform dependent library. This is true of c++ and Lua (which implements only ansi c functionality).
列表中的列表文件由平台定义,因此您必须使用平台相关库。 c ++和Lua(仅实现ansi c功能)也是如此。
#4
require "lfs"
function loadall(dir)
for file in lfs.dir(dir) do
if string.find(file, ".lua$") then
dofile(dir .. "/".. file)
end
end
end
#1
For Lua, you want the module Lua Filesystem.
对于Lua,您需要模块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.
正如Nick所观察到的那样,访问文件系统本身(而不是单个文件)超出了C和C ++标准的范围。由于Lua本身(用于实现C模块的require()的动态加载器除外)用标准C编写,因此核心语言缺少许多文件系统功能。
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.
但是,很容易扩展Lua核心,因为(几乎)任何具有文件系统的平台也支持DLL或共享库。 Lua文件系统是一个可移植的库,它增加了对目录迭代,文件属性发现等的支持。
With lfs, emulating some of the capability of DIR in Lua is essentially as simple as:
使用lfs,在Lua中模拟DIR的一些功能基本上就像:
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.
如果你的Lua副本来自Lua for Windows,那么你已经安装了lfs,上面的示例将开箱即用。
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 via luaL_dostring()
or by using the C API to retrieve the require()
function from the global table, push the string "lfs"
, and call the Lua function with something like lua_pcall(L,1,1,0)
, which leaves the lfs
table on the top of the Lua stack.
编辑:顺便说一下,Lua解决方案也可能是一个合理的C或C ++解决方案。 Lua核心并不大,提供动态的垃圾收集语言,并且很容易与C作为托管应用程序或扩展模块进行交互。要使用来自C应用程序的lfs,您将链接Lua DLL,初始化Lua状态,并通过luaL_dostring()或使用C API检索require()函数来获取执行require“lfs”的状态从全局表中,推送字符串“lfs”,并使用类似lua_pcall(L,1,1,0)的函数调用Lua函数,这将lfs表保留在Lua堆栈的顶部。
This approach probably makes the most sense if you already had a need for an embedded scripting language, and Lua meets your requirements.
如果您已经需要嵌入式脚本语言,并且Lua满足您的要求,这种方法可能最有意义。
#2
For a C++ solution, have a look at the Boost.Filesystem library.
对于C ++解决方案,请查看Boost.Filesystem库。
#3
Listing files in a directory is defined by the platform so you would have to use a platform dependent library. This is true of c++ and Lua (which implements only ansi c functionality).
列表中的列表文件由平台定义,因此您必须使用平台相关库。 c ++和Lua(仅实现ansi c功能)也是如此。
#4
require "lfs"
function loadall(dir)
for file in lfs.dir(dir) do
if string.find(file, ".lua$") then
dofile(dir .. "/".. file)
end
end
end