从变量字符串中提升文件系统路径
BOOST filesystem path from variable string
我在创建boost::filesystem::path
对象时遇到了这个问题(Boost V1.55)。我不知道如何从字符串变量或字符串串联创建路径?
//Example 1
namespace fs = boost::filesystem;
String dest = "C:/Users/username";
fs::path destination (dest); //Error here
//Example 2
namespace fs = boost::filesystem;
String user = "username";
fs::path destination ("C:/Users/" + user); //Error here as well.
//Example 3
namespace fs = boost::filesystem;
fs::path destination ("C:/Users/username");
在诸如示例3之类的双引号之间指定整个字符串时,我才能够创建一个路径对象,但这不允许使用变量输入。
基本上,如何使用字符串作为起点?
如何实现fs::path
对象类?
感谢您的任何帮助!
编辑
链接到Boost/Filesystem Path文档。重新学习C ,所以其中的一些仍然有点超过我的脑海...我不太了解构造函数在这里的工作方式...真的不知道该怎么问...。D绝对感谢任何指针。
谢谢gmannickg-您实际上设法解决了我的问题。我正在使用C 构建器10.1,并且能够与String
混乱一段时间,分配值等。实际上,ShowMessage()
方法使我找到了我的答案 - 在C 建造者中,它想要一个ansistring的论点来工作,工作,std ::字符串不会编译。C 构建器10.1将String
定义为ansistring,而不是std :: string。同样,我对C 很新,所以当using namespace std
我没有意识到差异时(我对OBJ的先验知识的大部分是来自Java,您将字符串定义为String
。
//Working Example in C++ Builder 10.1 Starter
namespace fs = boost::filesystem;
std::string un = "/username";
std::string dest = "C:/Users" + un; //concatenation test
fs::path destination (dest); //Works, no compiler error now
std::string pathStdString = destination.string(); //retrieve 'dest' as std:string from path
String pathAnsiString = pathStdString.c_str(); //Converts std::string to ansi
ShowMessage(pathAnsiString); //Output box showing the path (valid in C++ Builder)
希望这可以帮助其他人遇到类似问题。另外,链接到STD ::转换为ANSI,以防万一有人觉得有用。
相关文章:
- C++17 文件系统::remove_all 带有通配符路径
- 如何在不包含完整的文件系统头的情况下使用文件系统的类路径C++17?
- std::文件系统根路径如何将自身作为父路径?
- 如何将文件系统路径转换为字符串
- 返回混合 \ 和 / 的文件系统路径字符串方法
- Boost文件系统路径中的分割故障
- 在QT资源系统中,前缀比文件系统路径的优势是什么?
- C++ boost::文件系统如何检测路径是文件还是目录
- 从变量字符串中提升文件系统路径
- Simple_ls.cpp的C++Boost编译错误-未找到对Boost的引用::文件系统::路径::文件名
- 是否可以从一系列路径迭代器或子路径创建文件系统 TS basic_path
- C++:在比较 boost::文件系统中的路径时,如何忽略第一个目录路径
- 为什么路径比较在最新的文件系统草稿 (C++) 中区分大小写
- 如何创建文件路径可以像磁盘一样访问的虚拟文件系统
- 如何检查路径在 boost::文件系统中是否有效
- 如何在C++中编写一段代码以查找系统路径上文件的完整路径
- 在 Mac 上使用 boost::文件系统连接文件路径时程序崩溃
- 从 boost 1.48 升级到 1.49 具有文件系统路径访问冲突读取位置0x0000000
- boost::文件系统获取相对路径
- boost文件系统3路径包含检查