@Bergis

Как найти в локальной папке все файлы с нужным расширением?

Я попытался написать код , но он выдает ошибки :
#include <string>
#include <iostream>
#include <filesystem>
#include <stdio.h> 
#include <direct.h>
namespace fs = std::filesystem;



std::string check_dir()
{
    char current_work_dir[FILENAME_MAX];
    std::string path = _getcwd(current_work_dir, sizeof(current_work_dir));
    std::cout << path << std::endl;
    return path;
}

int main()
{
    std::string path2;
    std::string path;
    path = check_dir();
    for (const auto& entry : fs::directory_iterator(path))
        path2 = entry.path();
        if (path2.find('.cpp') != std::string::npos) {
            std::cout << path2 << '\n';
        }
        

}

Подскажите как можно их исправить
  • Вопрос задан
  • 1174 просмотра
Решения вопроса 1
0hquazEd
@0hquazEd
std::string extension = ".txt";
	std::vector<std::string> files;
	std::filesystem::directory_iterator iterator(std::filesystem::current_path());

	for (; iterator != std::filesystem::end(iterator); iterator++)
	{
		if (iterator->path().extension() == extension)
		{
			files.push_back(iterator->path().string());
		}
	}


Если вам нужны только имена файлов то path().string() -> path().filename().string()
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы