Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Filesystem library
Possible solution
A possible solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#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";
Enter to Rename, Shift+Enter to Preview
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content