Copying a directory_entry to a string variable?

Oct 14, 2019 at 1:40am
I've been trying to figure this out for a while.

How can I make a copy of a directory_entry variable but as a string data type?

A stack overflow post mentioned using the .string() method but using the format below it did not work:
std::string strVar = directEntryVar.string();

Also the directory_entry cppreference page doesn't say anything about it as far as I can see:
https://en.cppreference.com/w/cpp/filesystem/directory_entry

In summary, this function & it's child functions are opening many different files in the directory and opening them, storing data into program, closing the input files, then opening output files, and outputting the stored data of the program into the output files.

For Example, let's say the input file has:
"The:Code" { is great }
{ { "random gibberish" } }


Then the output file would be:
"The:Code"
"random gibberish"


Parent Function I'm referring to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void openFiles() {
    // 1.
    fs::path fullInPath = fs::current_path();
    fullInPath /= "InputFiles";

    // 2.
    for (const fs::directory_entry &inputFilename : fs::directory_iterator(fullInPath)) {
        // 2a.
        std::vector<std::string> alphaNums = {};

        // ----------------------------------------------
        // <No algorithm step in comments for this yet.>
        // Goal: Copy "InputFiles/<currentFileNameHere>" to a string so I can use it in argument functions as a string.
        std::string inputFilesLocStr = "InputFiles/";
        inputFilesLocStr += inputFilename;
        // ^ **Trying to make it add the filename to the str**
        // ----------------------------------------------

        // 2b.
        readInputFile(inputFilename, alphaNums);
        // 2c.
        writeToOutputFile(inputFilename, alphaNums);
    }
}


Help would be greatly appreciated.

Please Note: I hope you can help me without looking at my full code, as it's probably a mess atm. However, if you absolutely need to see my full code then this is it at the moment:
(Also the notes are mainly for me but you may find them useful)
https://pastebin.com/B25sGHC0
I put it in a pastebin because it didn't fit in this post (length limit) & cpp.sh doesn't compile it because it requires C++17.

Edit: Going to sleep for tonight. I hope to see this tomorrow but it may take me a few days depending how busy school is.
Last edited on Oct 14, 2019 at 2:40am
Oct 14, 2019 at 3:44am
> A stack overflow post mentioned using the .string() method but using the format below it did not work:
> std::string strVar = directEntryVar.string();

std::string strVar = directEntryVar.path().string();

For example:

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

// return a list of names of regular files in the directory 
std::vector<std::string> file_name_list( const std::string& path_to_dir )
{
    namespace fs = std::filesystem ;

    if( fs::is_directory(path_to_dir) )
    {
        std::vector<std::string> file_names ;

        for( const auto& entry : fs::directory_iterator(path_to_dir) )
            if( entry.is_regular_file() ) file_names.push_back( fs::absolute( entry.path() ).string() ) ;

        return file_names ;
    }

    else return {} ;  // not a directory; return empty vector
}

int main()
{
    const std::string directory = "C:\\Logs" ;

    for( const std::string& fname : file_name_list(directory) ) std::cout << fname << '\n' ;
}

Oct 15, 2019 at 1:54am
Ohh, thank you very much JLBorges!
Topic archived. No new replies allowed.