目录结构C ++

C:\Projects\Logs\RTC\MNH\Debug C:\Projects\Logs\FF 

是否有一个表达式/字符串,可以说直到找到“Logs”并打开它? (假设你总是低于它)

相同的可执行文件在不同的时间用完“Debug”,“MNH”或“FF”,可执行文件应始终将其日志文件保存到“Logs”中。

没有引用整个路径C:\ Projects \ Logs会有什么表达式?

谢谢。

听起来你问的是相对路径。

如果工作目录是C:\Projects\Logs\RTC\MNH\Debug\ ,则路径..\..\..\file表示Logs目录中的文件。

如果您可能位于C:\Projects\Logs\RTC\MNH\C:\Projects\Logs\RTC\MNH\Debug\ ,则没有单个表达式可以让您从任一位置返回Logs 。 您可以尝试检查..\..\..\..\Logs是否存在,如果不存在,请尝试..\..\..\Logs..\..\Logs..\Logs ,其中一个存在会告诉你你是多么“深刻”以及需要多少..才能让你回到Logs

您可能很幸运使用boost::filesystem库。

没有编译器(以及来自boost文档的忍者副本),例如:

 #include  namespace boost::filesystem = fs; bool contains_folder(const fs::path& path, const std::string& folder) { // replace with recursive iterator to check within // sub-folders. in your case you just want to continue // down parents paths, though typedef fs::directory_iterator dir_iter; dir_iter end_iter; // default construction yields past-the-end for (dir_iter iter(path); iter != end_iter; ++iter) { if (fs::is_directory(iter->status())) { if (iter->path().filename() == folder) { return true; } } } return false; } fs::path find_folder(const fs::path& path, const std::string& folder) { if (contains_folder(path, folder)) { return path.string() + folder; } fs::path searchPath = path.parent_path(); while (!searchPath.empty()) { if (contains_folder(searchPath, folder)) { return searchPath.string() + folder; } searchPath = searchPath.parent_path(); } return "": } int main(void) { fs::path logPath = find_folder(fs::initial_path(), "Log"); if (logPath.empty()) { // not found } } 

现在这是完全未经测试的:)

    Interesting Posts