I would like to use std::filesystem
in my project, which will allow me to show .txt
files in the current directory (I use Ubuntu, I don't need a Windows function because I have already seen one on StackOverflow).
Here is my GitHub repo:
https://github.com/jaroslawroszyk/-how-many-pages-per-day
I have a fix for this problem like this:
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);
}
}
But the code I enter here would like to use C++17, but I don't know how to find the .txt
files, now I wrote:
for (auto &fn : std::filesystem::directory_iterator("."))
if (std::filesystem::is_regular_file(fn))
{
std::cout << fn.path() << '\n';
}