我需要在 C++ 目录中生成一个文件名容器,它必须是跨平台兼容的。有人推荐使用 WIN32_FIND_DATA
数据结构。这是最佳选择吗?如果是,我将如何实现?
用户不会输入文件名,而是 C++ 函数会自动搜索目录并创建文件名容器。我也不知道如何读入这样的文件名。
我也非常关注标准,所以 <dirent.h>
这不是一个理想的解决方案,因为它不是 ISO C 标准,即使它是 C POSIX 库中的 header 。
最佳答案
我猜 WIN32_FIND_DATA 不是跨平台的最佳选择,但可能有一些库可以帮助在 Linux 上提供它。
考虑使用 boost filesystem将文件转储到 std::vector
.
类似这样的内容(改编自 here ):
void show_files(const path & directory)
{
if(!exists(directory)) return;
directory_iterator end ;
for( directory_iterator iter(directory) ; iter != end ; ++iter )
if (is_directory(*iter)) continue; // skip directories
cout << "File: " << iter->native_file_string() << "\n" ;
}
}
关于c++ - 创建一个包含目录中文件名的 C++ 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3113102/