SDL 或 C++ 中是否有任何工具允许您从文件夹中读取图像文件而不指定它们的名称,比如按顺序读取它们等?如果没有,您是否使用任何技术来完成相同的事情?
做这样的事情:
foo_ani[0] = LoadImage("Animations/foo1.png");
foo_ani[1] = LoadImage("Animations/foo2.png");
foo_ani[2] = LoadImage("Animations/foo3.png");
会变得非常乏味,并且不能使用循环,因为每次的文件名都是特定的。
我真正想到的唯一方法可能是拥有一个字符串,您可以通过每个循环迭代器修改该字符串,并将循环编号插入字符串的特定部分,假设您的文件是这样标记的,并将该字符串用作 LoadImage范围。与仅执行上述操作相比,这似乎需要更多工作。
最佳答案
此处显示的小程序列出了目录 files/
中的所有文件,与模式 fileN.type
匹配,其中 N 从 0 开始,未指定。
#include <iostream>
#include <sstream>
#include <string>
#include <boost/filesystem.hpp>
using namespace std;
namespace fs = boost::filesystem;
int main(int argc, char** argv)
{
fs::path dir ("./files");
string prefix = "file";
string suffix = "type";
int i = 0;
fs::path file;
do {
stringstream ss;
ss << prefix << i++ << "." << suffix;
file = fs::path(dir / fs::path(ss.str()));
if(fs::exists(file)) {
cout << file.leaf() << " exists." << endl;
}
} while(fs::exists(file));
return 0;
}
与 -lboost_filesystem
链接。
boost::filesystem
还提供了一个简单的目录迭代器。
关于c++ - 在不指定名称的情况下读取图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1551544/