Back
Close

Modern C++ idoms and recipes

meshell
9,924 views

Filesystem library

Possible solution

A possible solution
#include <experimental/filesystem>
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
namespace fs = std::experimental::filesystem;
using namespace std::string_literals;
/**
* List all regular files in a directory
* @param dir directory path
* @return vector of paths of regular files in directory
*/
std::vector<fs::path> list_regular_files_in_directory(const fs::path& dir) {
std::vector<fs::path> regular_files;
if (fs::exists(dir) && fs::is_directory(dir)) {
for (const auto& entry : fs::directory_iterator(dir)) {
if (fs::is_regular_file(entry.status())) {
regular_files.emplace_back(entry.path());
}
}
}
return regular_files;
}
/**
* Check if a file contains a particular string
* @param filepath path to the file that should be checked
* @param str string to search for in file
* @retval true if the string str was found in file filepath
* @retval false otherwise
*/
bool file_contains_string(const fs::path& filepath, const std::string& str) {
if (fs::exists(filepath)) {
std::ifstream file(filepath);
std::string line;
while (getline(file, line)) {
if (line.find(str) != std::string::npos) {
return true;
}
}
}
return false;
}
int main() {
const auto current_path = fs::current_path();
const auto data_path = current_path / "data";
const auto files = list_regular_files_in_directory(data_path);
std::cout << "Files in ./data: \n";
for (const auto& file_path : files) {
std::cout << file_path.filename() << std::endl;
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants