I can load a vector with the file names in a directory using other methods, but I'm working on learning to use #include <filesystem>. Instead of printing to the console, I want to add it to a vector. It sounded easy, but I'm running into a problem where the vector apparently does not recognize the filesystem output as a string. I cannot figure out how to convert it to a string.
Since I could not directly push it to the vector, I tried sending it to a function but the error I get is "no suitable user-defined conversion from const std::filesystem::path to std::string exists."
I also tried changing const auto& files in every conceivable way to be a string without success. I just cannot get vecArray and files.path() to be the same file type.
Is it possible to get the following to work or do I need to change my whole approach?
#include <iostream>
#include <string>
#include <vector>
#include <filesystem>
namespace fs = std::filesystem;
int main(){
std::string directoryPath{};
std::vector < std::string > vecArray{};
getline(std::cin, directoryPath);
for (const auto& files : fs::directory_iterator(directoryPath)) {
vecArray.push_back(files.path());
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire