windows C++ - 拆分文件名和文件扩展名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4314464/
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
C++ - Splitting Filename and File Extension
提问by Ruel
Ok, first of all I don't want to use Boost, or any external libraries. I just want to use the C++ Standard Library. I can easily split strings with a given delimiter with my split()
function:
好的,首先我不想使用 Boost 或任何外部库。我只想使用C++ 标准库。我可以使用我的split()
函数轻松拆分带有给定分隔符的字符串:
void split(std::string &string, std::vector<std::string> &tokens, const char &delim) {
std::string ea;
std::stringstream stream(string);
while(getline(stream, ea, delim))
tokens.push_back(ea);
}
I do this on filenames. But there's a problem. There are files that have extensions like: tar.gz
, tar.bz2
, etc. Also there are some filenames that have extra dots. Some.file.name.tar.gz
. I wish to separate Some.file.name
and tar.gz
Note:The number of dots in a filename isn't constant.
我在文件名上这样做。但是有一个问题。有些文件的扩展名为:tar.gz
、tar.bz2
等。还有一些文件名带有额外的点。Some.file.name.tar.gz
. 我想分开Some.file.name
并tar.gz
注意:文件名中的点数不是恒定的。
I also tried PathFindExtension
but no luck. Is this possible? If so, please enlighten me. Thank you.
我也试过,PathFindExtension
但没有运气。这可能吗?如果是这样,请赐教。谢谢你。
Edit: I'm very sorry about not specifying the OS. It's Windows.
编辑:我很抱歉没有指定操作系统。是窗户。
回答by icecrime
I think you could use std::string
find_last_of
to get the index of the last .
, and substr
to cut the string (although the "complex extensions" involving multiple dots will require additional work).
我认为您可以使用std::string
find_last_of
获取 last 的索引.
并substr
剪切字符串(尽管涉及多个点的“复杂扩展”将需要额外的工作)。
回答by etarion
There is no way of doing what you want that does not involve a database of extensions for your purpose. There's nothing magical about extensions, they are just part of a filename (if you gunzip foo.tar.gz
you'll likely get a foo.tar, so for this application .gz actually is "the extension"). So, in order to do what you want, build a database of extensions that you want to look for and fall back on "last dot" if you don't find one.
如果不涉及用于您的目的的扩展数据库,则无法做您想做的事情。扩展没有什么神奇之处,它们只是文件名的一部分(如果您gunzip foo.tar.gz
可能会得到一个 foo.tar,那么对于这个应用程序,.gz 实际上是“扩展名”)。所以,为了做你想做的事,建立一个你想要寻找的扩展数据库,如果你没有找到,就回到“最后一个点”。
回答by John Dibling
There's nothing in the C++ standard library -- that is, it's not in the Standard --, but every operating system I know of provides this functionality in a variety of ways.
C++ 标准库中没有任何内容——也就是说,它不在标准中——但是我所知道的每个操作系统都以各种方式提供此功能。
In Windows you can use _splitpath(), and in Linux you can use dirname() & basename()
在 Windows 中你可以使用 _splitpath(),在 Linux 中你可以使用 dirname() & basename()
回答by rubenvb
The problem is indeed filenames like *.tar.gz
, which can not be split consistently, due to the fact that (at least in Windows) the .tar
part isn't part of the extension. You'll either have to keep a list for these special cases and use a one-dot string::rfind
for the rest or find some pre-implemented way. Note that the .tar.*
extensions aren't infinite, and very much standardized (there's about ten of them I think).
这个问题确实是个文件名一样*.tar.gz
,不能被分割一致,由于这样的事实(在Windows中最少)的.tar
部分不是扩展的一部分。您要么必须为这些特殊情况保留一个列表,string::rfind
其余的使用一个点,要么找到一些预先实现的方法。请注意,.tar.*
扩展不是无限的,并且非常标准化(我认为大约有十个)。
回答by yasouser
You could create a look-up table of file extensions that you think you might encounter. And also add a command line option to add a new one to the look-up table if you encounter anything new. Then parse through the file name to see if it any entry in the look-up table is a sub-string in the file name.
您可以创建一个您认为可能会遇到的文件扩展名查找表。如果您遇到任何新问题,还可以添加一个命令行选项以将新选项添加到查找表中。然后通过文件名解析,看查找表中的任何条目是否是文件名中的子字符串。
EDIT: You can also refer to this question: C++/STL string: How to mimic regex like function with wildcards?
编辑:您还可以参考这个问题:C++/STL string: How to mi regex like function with wildcards?