C++ 如何将提升路径类型转换为字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4179322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:45:44  来源:igfitidea点击:

How to convert boost path type to string?

c++boostpath

提问by Johnston

Hello I currently have a program that gets a full path of a file's location and is put into a variable that is the type of: boost::filesystem2::path

您好,我目前有一个程序可以获取文件位置的完整路径,并将其放入类型为:boost::filesystem2::path 的变量中

I have looked up how to do this and have found that using:

我查了一下如何做到这一点,并发现使用:

string result1 = boost::filesystem::basename (myPath)

will convert the path to string BUT it only converts the file name (e.g. if the path is "C:\name\bobsAwesomeWordDoc.docx" it just returns "bobsAwesomeWordDoc").

将路径转换为字符串但它只转换文件名(例如,如果路径是“C:\name\bobsAwesomeWordDoc.docx”,它只返回“bobsAwesomeWordDoc”)。

I have found the following on how to convert the entire path to string, but I don't know how to implement it in my program. I have tried multiple ways but I am getting conversion errors.

我发现了以下关于如何将整个路径转换为字符串的内容,但我不知道如何在我的程序中实现它。我尝试了多种方法,但出现转换错误。

const std::string& string( ): This routine returns a copy of the string with which the path was initialized, with formatting per the path grammar rules.

const std::string& string():此例程返回初始化路径的字符串的副本,并根据路径语法规则进行格式化。

(found here)

(在这里找到)

I have tried:

我试过了:

string result1 = string& (myPath);

and a few other variations.

以及其他一些变体。

回答by icecrime

You just need to call myPath.string().

你只需要调用myPath.string().

回答by resigned

I believe you need to do a little more than just convert the path to a string - you should first obtain the canonical version of the path - an absolute path with no symbolic-link elements - and convert thatinto a string:

我相信你需要做的多一点,只是路径转换为字符串-你应该首先获得该路径的规范版本-无符号链接元素的绝对路径-并转换为一个字符串:

boost::filesystem::canonical(myPath).string();

P.S. - I've been programming with Boost for ages and I couldn't easily find this info in the docs.

PS - 我已经用 Boost 编程了很长时间,我无法在文档中轻松找到此信息。



Update (Oct 2017)

更新(2017 年 10 月)

Documentation: boost::filesystem::canonical.

文档:boost::filesystem::canonical

But note that as of C++17 there is std::filesystem, with canonicaland a lot more.

但请注意,从 C++17 开始,有std::filesystem规范等等。

回答by Paddy

This worked in wxWidgets: (I know I should just use the wx utilities but it is a test)

这在 wxWidgets 中有效:(我知道我应该只使用 wx 实用程序,但这是一个测试)

void WxWidgetsBoostTestFrame::OnTestBtnClick(wxCommandEvent& event)
{
    boost::filesystem::path currentPath;
    currentPath = boost::filesystem::current_path();
    std::string curDirString;
    curDirString = boost::filesystem::canonical(currentPath).string();
    wxString mystring(curDirString.c_str(), wxConvUTF8);
    wxMessageBox(mystring); // output:  C:/Users\client\Desktop...      
}

回答by J.Adler

Calling myPath.generic_string()will do what you need.

打电话myPath.generic_string()会做你需要的。

回答by Calorified

Do this

做这个

path.c_str();

path.c_str();

You should be fine.

你应该没事。