从变量字符串中提升文件系统路径

BOOST filesystem path from variable string

本文关键字:文件系统 路径 变量 字符串      更新时间:2023-10-16

我在创建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,以防万一有人觉得有用。