【发布时间】:2021-09-17 11:08:39
【问题描述】:
我想在我的项目中使用std::filesystem,这将允许我在当前目录中显示.txt文件(我使用Ubuntu,我不需要Windows函数,因为我已经在StackOverflow上看到了一个)。
这是我的 GitHub 存储库:
https://github.com/jaroslawroszyk/-how-many-pages-per-day
我有一个解决这个问题的方法:
void showFilesTxt()
{
DIR *d;
char *p1, *p2;
int ret;
struct dirent *dir;
d = opendir(".");
if (d)
{
while ((dir = readdir(d)) != NULL)
{
p1 = strtok(dir->d_name, ".");
p2 = strtok(NULL, ".");
if (p2 != NULL)
{
ret = strcmp(p2, "txt");
if (ret == 0)
{
std::cout << p1 << "\n";
}
}
}
closedir(d);
}
}
但是我这里输入的代码想用C++17,但是不知道怎么找到.txt文件,现在写了:
for (auto &fn : std::filesystem::directory_iterator("."))
if (std::filesystem::is_regular_file(fn))
{
std::cout << fn.path() << '\n';
}
【问题讨论】:
-
我假设C++17标签意味着你不能使用C++20库添加?
-
是的,你可以使用 c++20
标签: c++ c++17 txt std-filesystem