从文件名中获取目录名称

Jon*_*ury 76 c++ directory file

我有一个文件名(C:\ folder\foo.txt),我需要在非托管C++中检索文件夹名称(C:\文件夹).在C#中我会做这样的事情:

string folder = new FileInfo("C:\folder\foo.txt").DirectoryName;
Run Code Online (Sandbox Code Playgroud)

是否有一个可以在非托管C++中使用的函数从文件名中提取路径?

Ara*_*raK 146

使用Boost.Filesystem:

boost::filesystem::path p("C:\\folder\\foo.txt");
boost::filesystem::path dir = p.parent_path();
Run Code Online (Sandbox Code Playgroud)

  • `p.remove_filename()`将原位修改`p`,[可以比`p = p.parent_path()`更有效地实现)(http://www.boost.org/doc/libs/1_60_0 /libs/filesystem/doc/reference.html#path-remove_filename) (2认同)

cor*_*iKa 67

来自http://www.cplusplus.com/reference/string/string/find_last_of/的示例

// string::find_last_of
#include <iostream>
#include <string>
using namespace std;

void SplitFilename (const string& str)
{
  size_t found;
  cout << "Splitting: " << str << endl;
  found=str.find_last_of("/\\");
  cout << " folder: " << str.substr(0,found) << endl;
  cout << " file: " << str.substr(found+1) << endl;
}

int main ()
{
  string str1 ("/usr/bin/man");
  string str2 ("c:\\windows\\winhelp.exe");

  SplitFilename (str1);
  SplitFilename (str2);

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

  • 这是这里最好的最小解决方案。 (2认同)

And*_*and 23

PathRemoveFileSpec有一个标准的Windows功能.如果您仅支持Windows 8及更高版本,则强烈建议您使用PathCchRemoveFileSpec.在其他改进中,它不再限于MAX_PATH(260)个字符.

  • 请注意,此功能现已弃用.Microsoft的建议是使用[PathCchRemoveFileSpec](https://msdn.microsoft.com/en-us/library/hh707092%28v=vs.85%29.aspx). (2认同)

Ale*_*son 23

在C++ 17中,存在std::filesystem::path使用该方法的类parent_path.

#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
    for(fs::path p : {"/var/tmp/example.txt", "/", "/var/tmp/."})
        std::cout << "The parent path of " << p
                  << " is " << p.parent_path() << '\n';
}
Run Code Online (Sandbox Code Playgroud)

可能的输出:

The parent path of "/var/tmp/example.txt" is "/var/tmp"
The parent path of "/" is ""
The parent path of "/var/tmp/." is "/var/tmp"
Run Code Online (Sandbox Code Playgroud)

  • 还有一个 `.remove_filename()` 方法。 (3认同)
  • 谢谢,@Qqwy,它还允许使用该方法的目录路径来获得正确的预期结果,这与答案中的方法不同 (2认同)

tos*_*-cx 12

为什么它必须如此复杂?

#include <windows.h>

int main(int argc, char** argv)         // argv[0] = C:\dev\test.exe
{
    char *p = strrchr(argv[0], '\\');
    if(p) p[0] = 0;

    printf(argv[0]);                    // argv[0] = C:\dev
}
Run Code Online (Sandbox Code Playgroud)

  • 这不便携.linux中的路径分隔符是'/'.std :: filesystem :: path是标准的和可移植的. (9认同)

srb*_*ma1 8

 auto p = boost::filesystem::path("test/folder/file.txt");
 std::cout << p.parent_path() << '\n';             // test/folder
 std::cout << p.parent_path().filename() << '\n';  // folder
 std::cout << p.filename() << '\n';                // file.txt
Run Code Online (Sandbox Code Playgroud)

您可能需要p.parent_path().filename()获取父文件夹的名称。


Edw*_*nge 5

使用boost :: filesystem.无论如何它将被纳入下一个标准,所以你也可以习惯它.

  • "无论如何,它将被纳入下一个标准"而事实并非如此 (7认同)
  • @AlessandroJacopson很酷,它似乎包含在C++ 17中 - http://en.cppreference.com/w/cpp/filesystem (5认同)