C++17 Filesystem Problem - How to iterate current directory?

Program Purpose: Let's say I have 300 files in a directory. I want to take all the info from every file and store it into a single output file.

Problem:
How can I iterate through the current directory to open each file one at a time, do actions, and close them? I'm using C++17.


In short:
- Have checked cppreference, stackoverflow, cplusplus, and:
https://www.codingame.com/playgrounds/5659/c17-filesystem
Although I guess I'm just blanking because I can't seem to figure this out... I feel like I'm missing something obvious..

1
2
3
4
5
6
7
8
9
10
11
12
13
    // 1. Set path all files are in.
    //fs::path p("C:\\Users\\Tom\\Desktop\\Anti-Shop-Exploits-v2\\1.15_Data\\recipes");
    
    // 2. Open one file at a time using directory_iterator.
    std::vector<std::string> fileLines;
    for (fs::directory_entry &elem : fs::directory_iterator()) {
    //for ( auto &elem : fs::directory_iterator(p) ) {
        // 3a. Open file and make sure it is actually open. If not, send error msg and stop program.
        std::ifstream inFile(elem);
        inFile.open(elem);
    
        // ...
    }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void storeDataInNewFile(std::vector<std::string> &v) {
    // Declare path.
    fs::path filePath("C:\\Users\\Tom\\Desktop\\Anti-Shop-Exploits-v2\\1.15_Data\\recipes\\allData.json"); // Absolute (Full) Path

    // Open path / output file.
    std::ofstream outFile(fs::absolute(filePath));

    // Make sure it is open.
    // <.. code>

    // Store all data from vector into output file using a loop.
    const int vSize = v.size();
    for (int i = 0; i < vSize; ++i) {
        outFile << v[i];
    }
    // ...

    outFile.close();
}


Full Code: https://pastebin.com/iPVRCtm6
Error: https://pastebin.com/H3MS2rhd

Help would be greatly appreciated. Thank you!
Last edited on
I haven't actually compiled this but I think it should be more like this.

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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <filesystem>

namespace fs = std::filesystem;

void getInFileData(const std::string& path, std::vector<std::string>& v);
void storeDataInNewFile(const std::vector<std::string>& v);

const std::string Base = "C:/Users/Tom/Desktop/Anti-Shop-Exploits-v2/1.15_Data/recipes";

int main() {
    fs::path p(Base);
    for ( auto &elem : fs::directory_iterator(p) ) {
        std::vector<std::string> fileLines;
        getInFileData(elem.path(), fileLines);
        storeDataInNewFile(fileLines);
    }
}

void getInFileData(const std::string& path, std::vector<std::string>& v) {
    std::ifstream in(path);
    if (!in) {
        std::cout << "[Error] Could not open input file: " << path << '\n';
        std::exit(1);
    }
    std::string line;
    while (std::getline(in, line)) v.push_back(line);
    // do not explicitly close file; let the ifstream's dtor do it
}
 
void storeDataInNewFile(const std::vector<std::string>& v) {
    fs::path filePath(Base + "/allData.json");
    std::ofstream out(fs::absolute(filePath), std::ios::app); // you want to append
    if (!out) {
        std::cout << "[Error] Could not open output file: " << filePath << '\n';
        std::exit(1);
    }
    for (const auto& line: v)
        out << line << '\n';
    out << '\n';
}


Or you don't even need the vector or need to open the output file over and over.

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
#include <iostream>
#include <string>
#include <fstream>
#include <filesystem>

namespace fs = std::filesystem;

void writeFileToStream(const std::string& path, std::ofstream& out);

const std::string Base = "C:/Users/Tom/Desktop/Anti-Shop-Exploits-v2/1.15_Data/recipes";

int main() {
    fs::path p(Base);

    fs::path filePath(Base + "/allData.json");
    std::ofstream out(fs::absolute(filePath));
    if (!out) {
        std::cout << "[Error] Could not open output file: " << filePath << '\n';
        std::exit(1);
    }

    for ( auto &elem : fs::directory_iterator(p) )
        writeFileToStream(elem.path(), out);
}

void writeFileToStream(const std::string& path, std::ofstream& out)
{
    std::ifstream in(path);
    if (!in) {
        std::cout << "[Error] Could not open input file: " << path << '\n';
        std::exit(1);
    }
    std::string line;
    while (std::getline(in, line)) out << line << '\n';
}

Last edited on
Apologies for the late reply! Thank you for being so detailed! I will read your full reply later today when I have the chance to work on the program! :)
Topic archived. No new replies allowed.