C++ 从文件名中获取目录名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3071665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting a directory name from a filename
提问by Jon Tackabury
I have a filename (C:\folder\foo.txt) and I need to retrieve the folder name (C:\folder) in unmanaged C++. In C# I would do something like this:
我有一个文件名 (C:\folder\foo.txt),我需要在非托管 C++ 中检索文件夹名称 (C:\folder)。在 C# 中,我会做这样的事情:
string folder = new FileInfo("C:\folder\foo.txt").DirectoryName;
Is there a function that can be used in unmanaged C++ to extract the path from the filename?
是否有可以在非托管 C++ 中使用的函数来从文件名中提取路径?
采纳答案by Andreas Rejbrand
There is a standard Windows function for this, PathRemoveFileSpec. If you only support Windows 8 and later, it is highly recommended to use PathCchRemoveFileSpecinstead. Among other improvements, it is no longer limited to MAX_PATH
(260) characters.
为此,有一个标准的 Windows 函数PathRemoveFileSpec。如果您只支持 Windows 8 及更高版本,强烈建议改用PathCchRemoveFileSpec。在其他改进中,它不再限于MAX_PATH
(260) 个字符。
回答by AraK
Using Boost.Filesystem:
使用 Boost.Filesystem:
boost::filesystem::path p("C:\folder\foo.txt");
boost::filesystem::path dir = p.parent_path();
回答by corsiKa
Example from http://www.cplusplus.com/reference/string/string/find_last_of/
来自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;
}
回答by Alessandro Jacopson
In C++17 there exists a class std::filesystem::path
using the method parent_path
.
在 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';
}
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"
回答by toster-cx
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
}
回答by Edward Strange
Use boost::filesystem. It will be incorporated into the next standard anyway so you may as well get used to it.
使用 boost::filesystem。无论如何,它将被纳入下一个标准,因此您最好习惯它。
回答by srbcheema1
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.
您可能需要p.parent_path().filename()
获取父文件夹的名称。
回答by Ofek Shilon
_splitpathis a nice CRT solution.
_splitpath是一个不错的 CRT 解决方案。
回答by Utkarsh Kumar
I'm so surprised no one has mentioned the standard way in Posix
我很惊讶没有人提到 Posix 中的标准方式
Please use basename / dirname
constructs.
请使用basename / dirname
构造。
man basename
人基名
回答by Cogwheel
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.
在这方面,标准 C++ 对您没有多大帮助,因为路径名是特定于平台的。您可以手动解析字符串(如glowcoder 的回答),使用操作系统工具(例如http://msdn.microsoft.com/en-us/library/aa364232(v=VS.85).aspx),或者可能是最好的方法是,您可以使用像 boost::filesystem 这样的第三方文件系统库。