简体   繁体   English

目录结构C ++

[英]directory structures C++

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

Is there an expression/string that would say go back until you find "Logs" and open it? 是否有表达式/字符串可以说可以追溯到找到“ Logs”并打开它? (assuming you were always below it) (假设您一直在下面)

The same executable is run out of "Debug", "MNH" or "FF" at different times, the executable always should save it's log files into "Logs". 相同的可执行文件在不同的时间用完了“调试”,“ MNH”或“ FF”,可执行文件始终应将其日志文件保存到“日志”中。

What expression would get there WITHOUT referring to the entire path C:\\Projects\\Logs? 如果不引用整个路径C:\\ Projects \\ Logs,将得到什么表达式?

Thanks. 谢谢。

You might have luck using the boost::filesystem library. 使用boost::filesystem库可能很幸运。

Without a compiler (and ninja-copies from boost documentation), something like: 如果没有编译器(以及boost文档中的ninja副本),则类似:

#include <boost/filesystem.hpp>

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
    }
}

For now this is completely untested :) 现在,这是完全未经测试的:)

It sounds like you're asking about a relative path. 听起来您在询问相对路径。

If the working directory is C:\\Projects\\Logs\\RTC\\MNH\\Debug\\ , the path ..\\..\\..\\file represents a file in the Logs directory. 如果工作目录为C:\\Projects\\Logs\\RTC\\MNH\\Debug\\ ,则路径..\\..\\..\\file表示Logs目录中的文件。

If you might be in either C:\\Projects\\Logs\\RTC\\MNH\\ or C:\\Projects\\Logs\\RTC\\MNH\\Debug\\ , then no single expression will get you back to Logs from either place. 如果您可能位于C:\\Projects\\Logs\\RTC\\MNH\\C:\\Projects\\Logs\\RTC\\MNH\\Debug\\ ,那么任何一个表达式都不会使您从任一位置返回到Logs You could try checking for the existence of ..\\..\\..\\..\\Logs and if that doesn't exist, try ..\\..\\..\\Logs , ..\\..\\Logs and ..\\Logs , which one exists would tell you how "deep" you are and how many .. s are required to get you back to Logs . 您可以尝试检查是否存在..\\..\\..\\..\\Logs ,如果不存在,请尝试..\\..\\..\\Logs..\\..\\Logs..\\Logs (存在)会告诉您您有多“深”,以及需要多少..才能使您回到Logs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM