如何解决尝试将 <filesystem> 输出转换为字符串
我可以使用其他方法在目录中加载带有文件名的向量,但我正在学习使用#include
由于我无法直接将其推送到向量,因此我尝试将其发送到函数,但得到的错误是“不存在从 const std::filesystem::path 到 std::string 的合适的用户定义转换。 "
我还尝试以各种可能的方式将 const auto& 文件更改为字符串,但没有成功。我只是无法让 vecArray 和 files.path() 成为相同的文件类型。
是否有可能使以下内容起作用,或者我是否需要改变我的整个方法?
#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;
}
解决方法
使用方法 c_str()
或 string()
std::filesystem::path::string
vecArray.push_back(files.path().c_str());
或
vecArray.push_back(files.path().string());
,
最好使用std::vector <std::filesystem::path> ..
或使用 std::wstring
。后者看起来并不那么容易编码
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。