从文件路径c ++获取目录

nid*_*hal 19 c++ visual-c++

获取文件所在目录的最简单方法是什么?我正在使用它来查找工作目录.

string filename = "C:\MyDirectory\MyFile.bat" 
Run Code Online (Sandbox Code Playgroud)

在这个例子中,我应该得到"C:\ MyDirectory".

hmj*_*mjd 18

初始化不正确,因为您需要转义反斜杠:

string filename = "C:\\MyDirectory\\MyFile.bat";
Run Code Online (Sandbox Code Playgroud)

要提取目录(如果存在):

string directory;
const size_t last_slash_idx = filename.rfind('\\');
if (std::string::npos != last_slash_idx)
{
    directory = filename.substr(0, last_slash_idx);
}
Run Code Online (Sandbox Code Playgroud)

  • 如果文件名(合法地)使用*forward*slashes则失败.如果文件名是相对的,也无法获取(绝对)目录.还依赖于正确设置的编码.当有Offirmo(便携式)Boost解决方案时,我不会将此标记为"正确". (2认同)

Off*_*rmo 18

使用Boost.filesystem parent_path()函数.

防爆.参数c:/ foo/bar => c:/ foo

更多的例子在这里:路径分解表和教程这里.

  • 我想知道所有其他DIY错误的答案 (3认同)
  • +1.这是唯一可以被认为是正确的. (3认同)
  • 并不是每个人都使用 boost。 (2认同)

seh*_*ehe 16

快速又脏:

请注意,您还必须查找,/因为在Windows上允许使用备用路径分隔符

#include <string>
#include <iostream>

std::string dirnameOf(const std::string& fname)
{
     size_t pos = fname.find_last_of("\\/");
     return (std::string::npos == pos)
         ? ""
         : fname.substr(0, pos);
}

int main(int argc, const char *argv[])
{
     const std::string fname = "C:\\MyDirectory\\MyFile.bat";

     std::cout << dirnameOf(fname) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)


Ste*_*ric 12

MFC方式;

#include <afx.h>

CString GetContainingFolder(CString &file)
{
    CFileFind fileFind;
    fileFind.FindFile(file);
    fileFind.FindNextFile();
    return fileFind.GetRoot();
}
Run Code Online (Sandbox Code Playgroud)

或者,甚至更简单

CString path(L"C:\\my\\path\\document.txt");
path.Truncate(path.ReverseFind('\\'));
Run Code Online (Sandbox Code Playgroud)


ger*_*rdw 10

C++17 提供了std::filesystem::path。它可能在 C++11 中可用;与 -lstdc++fs 链接。请注意,该函数不会验证路径是否存在;使用 std::filesystem::status 确定文件类型(可能是 filetype::notfound)


jha*_*sse 9

从 C++17 开始,您可以使用std::filesystem::parent_path

#include <filesystem>
#include <iostream>

int main() {
    std::string filename = "C:\\MyDirectory\\MyFile.bat";
    std::string directory = std::filesystem::path(filename).parent_path().u8string();
    std::cout << directory << std::endl;
}
Run Code Online (Sandbox Code Playgroud)


Aji*_*aze 5

您可以使用stdlib.h头文件中提供的_spliltpath函数.请参阅此链接.

http://msdn.microsoft.com/en-us/library/e737s6tf%28v=vs.71%29.aspx


小智 5

一个非常简单的跨平台解决方案(改编自此示例string::find_last_of):

std::string GetDirectory (const std::string& path)
{
    size_t found = path.find_last_of("/\\");
    return(path.substr(0, found));
}
Run Code Online (Sandbox Code Playgroud)

这适用于斜杠可以向后或向前指向(或混合)的两种情况,因为它只是查找 string 中最后一次出现的斜杠path

然而,我个人更喜欢使用 Boost::Filesystem 库来处理这样的操作。一个例子:

std::string GetDirectory (const std::string& path)
{
    boost::filesystem::path p(path);
    return(p.parent_path().string());
}
Run Code Online (Sandbox Code Playgroud)

不过,如果从字符串获取目录路径是您需要的唯一功能,那么 Boost 可能有点矫枉过正(特别是因为 Boost::Filesystem 是少数几个非头文件的 Boost 库之一)。但是,AFIK、Boost::Filesystem 已被批准纳入 TR2 标准,但可能要等到 C++14 或 C++17 标准(可能是后者,基于此答案)才能完全可用,因此取决于在您的编译器上(当您阅读本文时),您甚至可能不再需要单独编译它们,因为它们可能已经包含在您的系统中。例如,Visual Studio 2012 已经可以使用一些 TR2 文件系统组件(根据这篇文章),尽管我还没有尝试过,因为我仍在使用 Visual Studio 2010...


Him*_*shu 5

由于问题已经很久了,但我想添加一个答案,以便对其他人有所帮助。
在Visual c ++中,您也可以使用CString或char数组

CString filename = _T("C:\\MyDirectory\\MyFile.bat");  
PathRemoveFileSpec(filename); 
Run Code Online (Sandbox Code Playgroud)

输出:

C:\ MyDirectory

包括Shlwapi.h在头文件中。

MSDN LINK在这里您可以检查示例。