C:\Projects\Logs\RTC\MNH\Debug
C:\Projects\Logs\FF
有沒有一個表達式/字符串,會說回去,直到你找到「日誌」並打開它? (假設你總是在它下面)目錄結構C++
相同可執行文件在不同時間用完「Debug」,「MNH」或「FF」,可執行文件總是應該將它的日誌文件保存到「日誌」中。
什麼表達式將不會引用整個路徑C:\ Projects \ Logs?
謝謝。
C:\Projects\Logs\RTC\MNH\Debug
C:\Projects\Logs\FF
有沒有一個表達式/字符串,會說回去,直到你找到「日誌」並打開它? (假設你總是在它下面)目錄結構C++
相同可執行文件在不同時間用完「Debug」,「MNH」或「FF」,可執行文件總是應該將它的日誌文件保存到「日誌」中。
什麼表達式將不會引用整個路徑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
庫運氣。
沒有一個編譯器(以及從升壓文檔忍者副本),是這樣的:
#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
}
}
現在這是完全未經測試:)