![](/img/trans.png)
[英]Boost library, using std::wstring as filename with boost::property_tree::read_xml
[英]Storing filename got using boost library in std::string variable
我想使用 boost::filesystem 獲取目錄中所有文件的列表
我可以使用cout
打印文件名,但我無法將文件名存儲在字符串變量中。 我也嘗試過類型轉換和 strcpy,但沒有一種方法有效。
以下是代碼:
char dir[100] = "/home/harsh/";
namespace fs = boost::filesystem;
fs::directory_iterator start = fs::directory_iterator(dir);
fs::directory_iterator di = start;
for (; di != fs::directory_iterator(); ++di)
{
std::cout << "hello .. " << di->path() << std::endl;
//std::string strHarsh = di->path(); //Error
}
嘗試di->leaf()
它應該轉換為字符串
此外,這取決於您的 boost 版本,如果您使用的是文件系統 v3,它將是: di->path().filename().string()
您可以使用std::ostringstream
作為中間:
std::ostringstream os;
os << di->path();
std::string path = os.str();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.