简体   繁体   English

提升文件系统,需要帮助了解我正在做什么

[英]Boost FileSystems, need help understanding what I'm doing

I wrote a program using boost filesystems almost a year ago, and I am now trying to go back and use that for a reference, but I'm not sure exactly what is going on with the code, and if there might be a better way to do it. 我差不多一年前使用boost文件系统编写了一个程序,现在我想回去使用它作为参考,但我不确定代码究竟发生了什么,如果有更好的方法去做吧。

Here is what I had done to iterate through a directory. 这是我在迭代目录时所做的工作。

vector <directory_entry> entry;
copy(directory_iterator("path"), directory_iterator(), back_inserter(entry));

This gets me a vector with directory entries for all of the files and directories inside the directory at "path" 这给我一个带有目录条目的向量,用于“path”目录中的所有文件和目录

Then I would sort them into two vectors of paths, one for files, one for directories, using is_regular_file. 然后我将它们分为两个路径向量,一个用于文件,一个用于目录,使用is_regular_file。

I was working with openAL, and I would have to do this type of conversion to get things working. 我正在使用openAL,我必须进行这种类型的转换才能使事情正常进行。

path fp = file[0]; //file a vector of directory_entry
string fps = fp.string();
buffer[0] = AlutCreateBufferFromFile(fps.c_str());

And this worked, but I am thinking this all must not be very correct how I'm using it. 这有效,但我认为这一切都不能正确我如何使用它。 Anyways, I just wanted to see if anyone could give me a little advice. 无论如何,我只是想看看是否有人可以给我一些建议。

Your code looks correct. 您的代码看起来正确。

You can avoid a couple variable declarations like so: 你可以避免像这样的几个变量声明:

buffer[0] = AlutCreateBufferFromFile(file[0].string().c_str());

You can also avoid copying the directory entries into a vector and iterate over the directory entries directly: 您还可以避免将目录条目复制到vector并直接遍历目录条目:

directory_iterator cur("path");
directory_iterator end;
while (cur != end)
{
    path p = *cur;
    ...  // use p
    ++cur;
}

In C++11, you can improve on the loop further with lambdas: 在C ++ 11中,您可以使用lambdas进一步改进循环:

for_each(directory_iterator("path"), directory_iterator(), [](const path& p)
{
    // use p
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM