将Boost FileSystem3迭代器强制转换为常量字符* 我使用Boost文件系统3在目录中循环文件,我需要将文件名转换成另一个LIB的字符,不幸的是我的C++ FoO缺少,有人能帮助吗?p> int main(int argc, char* argv[]) { path p (argv[1]); // p reads clearer than argv[1] in the following code try { if (exists(p)) // does p actually exist? { if (is_regular_file(p)) // is p a regular file? cout << p << " size is " << file_size(p) << '\n'; else if (is_directory(p)) // is p a directory? { cout << p << " is a directory containing:\n"; typedef vector<path> vec; // store paths, vec v; // so we can sort them later copy(directory_iterator(p), directory_iterator(), back_inserter(v)); sort(v.begin(), v.end()); // sort, since directory iteration // is not ordered on some file systems for (vec::const_iterator it (v.begin()); it != v.end(); ++it) { cout << " " << *it << '\n'; /****************** stuck here **************************/ // I need to cast *it to a const char* filename /****************** stuck here **************************/ } } else cout << p << " exists, but is neither a regular file nor a directory\n"; } else cout << p << " does not exist\n"; } catch (const filesystem_error& ex) { cout << ex.what() << '\n'; } return 0; } intmain(intargc,char*argv[]) { 路径p(argv[1]);//在下面的代码中,p读起来比argv[1]清晰 尝试 { if(exists(p))//p实际上存在吗? { if(is_regular_file(p))//p是常规文件吗? cout

将Boost FileSystem3迭代器强制转换为常量字符* 我使用Boost文件系统3在目录中循环文件,我需要将文件名转换成另一个LIB的字符,不幸的是我的C++ FoO缺少,有人能帮助吗?p> int main(int argc, char* argv[]) { path p (argv[1]); // p reads clearer than argv[1] in the following code try { if (exists(p)) // does p actually exist? { if (is_regular_file(p)) // is p a regular file? cout << p << " size is " << file_size(p) << '\n'; else if (is_directory(p)) // is p a directory? { cout << p << " is a directory containing:\n"; typedef vector<path> vec; // store paths, vec v; // so we can sort them later copy(directory_iterator(p), directory_iterator(), back_inserter(v)); sort(v.begin(), v.end()); // sort, since directory iteration // is not ordered on some file systems for (vec::const_iterator it (v.begin()); it != v.end(); ++it) { cout << " " << *it << '\n'; /****************** stuck here **************************/ // I need to cast *it to a const char* filename /****************** stuck here **************************/ } } else cout << p << " exists, but is neither a regular file nor a directory\n"; } else cout << p << " does not exist\n"; } catch (const filesystem_error& ex) { cout << ex.what() << '\n'; } return 0; } intmain(intargc,char*argv[]) { 路径p(argv[1]);//在下面的代码中,p读起来比argv[1]清晰 尝试 { if(exists(p))//p实际上存在吗? { if(is_regular_file(p))//p是常规文件吗? cout,c++,boost,casting,C++,Boost,Casting,表达式*返回类型为path的对象,因此您必须: const std::string & s = (*it).string(); const char *str = s.c_str(); //this is what you want 或者,您可能希望使用其他转换函数,如下所示: const std::string & string() const; std::string native_file_string() const; std::string native_direc

表达式
*返回类型为
path
的对象,因此您必须:

const std::string & s = (*it).string(); 
const char *str = s.c_str(); //this is what you want
或者,您可能希望使用其他转换函数,如下所示:

const std::string & string() const;
std::string native_file_string() const;
std::string native_directory_string() const;
选择要使用的选项。首先阅读文档,了解每个选项返回的内容:


链接可能会转到boost.org上的canonical。有两个问题……首先,
s
是否比表达式
const std::string&s=(*it).string();
?其次,
str
和内存支持
c_str()
是否比表达式
const char*str=s.c_str();
更有效?(这听起来像是
stringstream.str()
和必须复制的
std::string
。@1201programalam-Re:这个封闭的问题在…你知道这个答案是安全的还是准确的吗?字符串引用让我担心。我花了两天的大部分时间调试和学习
stringstream.str()
lesson.@jww这是安全的,只要你不让指针在字符串超出范围后被保留。我怀疑你学到的“
stringstream.str()
lesson”在某些方面是不准确的(一些细微的细节可能被忽略了)。也许这是一个新问题?@jww不,这个答案是按照那边的答案告诉你的:将临时变量赋值给一个变量,然后在单独的语句中调用
c_str()
。另请参阅。