从文件名获取目录名

发布于 2024-09-06 04:33:38 字数 233 浏览 8 评论 0 原文

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

string folder = new FileInfo("C:\folder\foo.txt").DirectoryName;

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

I have a filename (C:\folder\foo.txt) and I need to retrieve the folder name (C:\folder) in C++. In C# I would do something like this:

string folder = new FileInfo("C:\folder\foo.txt").DirectoryName;

Is there a function that can be used in C++ to extract the path from the filename?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

我们的影子 2024-09-13 04:33:38

使用 Boost.Filesystem:

boost::filesystem::path p("C:\\folder\\foo.txt");
boost::filesystem::path dir = p.parent_path();

Using Boost.Filesystem:

boost::filesystem::path p("C:\\folder\\foo.txt");
boost::filesystem::path dir = p.parent_path();
傾旎 2024-09-13 04:33:38

示例来自 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;
}

Example from 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;
}
夜血缘 2024-09-13 04:33:38

在 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';
}

可能的输出:

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"

In C++17 there exists a class std::filesystem::path using the method 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';
}

Possible output:

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"
俯瞰星空 2024-09-13 04:33:38

为此有一个标准的 Windows 函数,PathRemoveFileSpec。如果您仅支持 Windows 8 及更高版本,强烈建议使用 PathCchRemoveFileSpec 相反。除其他改进外,它不再限于 MAX_PATH (260) 个字符。

There is a standard Windows function for this, PathRemoveFileSpec. If you only support Windows 8 and later, it is highly recommended to use PathCchRemoveFileSpec instead. Among other improvements, it is no longer limited to MAX_PATH (260) characters.

故笙诉离歌 2024-09-13 04:33:38

为什么一定要这么复杂?

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

Why does it have to be so complicated?

#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
}
七颜 2024-09-13 04:33:38
 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

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

 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

You may need p.parent_path().filename() to get name of parent folder.

脸赞 2024-09-13 04:33:38

使用 boost::文件系统。无论如何,它都会被纳入下一个标准,所以您最好习惯它。

Use boost::filesystem. It will be incorporated into the next standard anyway so you may as well get used to it.

路还长,别太狂 2024-09-13 04:33:38

我很惊讶没有人提到 Posix 中的标准方式

请使用 basename / dirname 构造。

男子基本名

I'm so surprised no one has mentioned the standard way in Posix

Please use basename / dirname constructs.

man basename

以酷 2024-09-13 04:33:38

_splitpath 是一个很好的 CRT 解决方案。

_splitpath is a nice CRT solution.

柳絮泡泡 2024-09-13 04:33:38

标准 C++ 在这方面不会为您做太多事情,因为路径名是特定于平台的。您可以手动解析字符串(如glowcoder的答案),使用操作系统工具(例如 http://msdn.microsoft.com/en-us/library/aa364232(v=VS.85).aspx ),或者可能是最好的方法,您可以使用第三方文件系统库,如 boost::filesystem。

Standard C++ won't do much for you in this regard, since path names are platform-specific. You can manually parse the string (as in glowcoder's answer), use operating system facilities (e.g. http://msdn.microsoft.com/en-us/library/aa364232(v=VS.85).aspx ), or probably the best approach, you can use a third-party filesystem library like boost::filesystem.

初见 2024-09-13 04:33:38

只需使用这个:ExtractFilePath(your_path_file_name)

Just use this: ExtractFilePath(your_path_file_name)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文