2012-12-30 36 views
0
for (fs::directory_iterator iter(realPath); iter != end_iter; ++iter) 
     { 
      if (iter->path().extension() == ".png"){ 
       fs::path currentPath = iter->path(); 
       const char *filename = const_cast<char*>(currentPath.string().c_str()); 
       std::cout << iter->path().leaf() << std::endl; 

       processFile(filename); 
       std::cout <<"Hi" << std::endl; 
      } 
     } 

这是我的代码,这里的processFile函数接受char *格式的文件名。以上代码返回文件名的垃圾值。不知道获取文件名的最佳方式是什么。如何从boost路径获取char *文件名

+1

你为什么要铸造在这一行? ''const char * filename = const_cast (currentPath.string()。c_str());'''''''''''''c_str()const char * ' –

回答

0

它适用于我这里(Boost.1.52,Mac OS X,clang)。

虽然我不得不同意米格尔,但你在这里做了很多工作。

for (fs::directory_iterator iter(realPath); iter != end_iter; ++iter) 
    if (iter->path().extension() == ".png") { 
     fs::path currentPath = iter->path(); 
     std::cout << currentPath.leaf() << std::endl; 
     processFile(currentPath.c_str()); 
    } 

,并根据代码你的口味,你可以摆脱currentPath共(与iter->path()更换)