Jef*_*ffV 73 c++ string filenames file-extension
给定一个字符串"filename.conf"
,如何验证扩展部分?
我需要一个跨平台的解决方案.
bri*_*man 142
这个解决方案太简单了吗?
#include <iostream>
#include <string>
int main()
{
std::string fn = "filename.conf";
if(fn.substr(fn.find_last_of(".") + 1) == "conf") {
std::cout << "Yes..." << std::endl;
} else {
std::cout << "No..." << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
Tor*_*ack 41
最好的方法是不要编写任何代码,而是调用现有方法.在Windows中,PathFindExtension方法可能是最简单的.
那你为什么不写自己的呢?
那么,以strrchr为例,当您在以下字符串"c:\ program files\AppleGate.Net\readme"上使用该方法时会发生什么?".Net\readme"是扩展名吗?编写适用于少数示例情况的内容很容易,但编写适用于所有情况的内容可能要困难得多.
小智 34
您必须确保使用多个点来处理文件名.示例:或者c:\.directoryname\file.name.with.too.many.dots.ext
无法正确处理strchr
find.
我最喜欢的是具有扩展(路径)功能的boost文件系统库
17 *_* 26 30
假设您有权访问STL:
std::string filename("filename.conf");
std::string::size_type idx;
idx = filename.rfind('.');
if(idx != std::string::npos)
{
std::string extension = filename.substr(idx+1);
}
else
{
// No extension found
}
Run Code Online (Sandbox Code Playgroud)
编辑:这是一个跨平台解决方案,因为您没有提到平台.如果您专门在Windows上,则需要利用该线程中其他人提到的Windows特定功能.
pet*_*sev 24
其他人提到了提升,但我只想添加实际代码来执行此操作:
#include <boost/filesystem.hpp>
using std::string;
string texture = foo->GetTextureFilename();
string file_extension = boost::filesystem::extension(texture);
cout << "attempting load texture named " << texture
<< " whose extensions seems to be "
<< file_extension << endl;
// Use JPEG or PNG loader function, or report invalid extension
Run Code Online (Sandbox Code Playgroud)
小智 19
实际上STL可以在没有太多代码的情况下做到这一点,我建议你学习一下STL,因为它可以让你做一些奇特的事情,无论如何这就是我使用的东西.
std::string GetFileExtension(const std::string& FileName)
{
if(FileName.find_last_of(".") != std::string::npos)
return FileName.substr(FileName.find_last_of(".")+1);
return "";
}
Run Code Online (Sandbox Code Playgroud)
即使在像"this.abcdesmp3"这样的字符串上,如果找不到它将返回的扩展名"",此解决方案也将始终返回扩展名.
实际上,最简单的方法是
char* ext;
ext = strrchr(filename,'.')
Run Code Online (Sandbox Code Playgroud)
要记住一件事:如果'.'
文件名中不存在,则ext将是NULL
.
使用C ++ 17及其代码std::filesystem::path::extension
(该库是boost :: filesystem的后继程序),您的语句将比使用eg更具表达力std::string
。
#include <iostream>
#include <filesystem> // C++17
namespace fs = std::filesystem;
int main()
{
fs::path filePath = "my/path/to/myFile.conf";
if (filePath.extension() == ".conf") // Heed the dot.
{
std::cout << filePath.stem() << " is a valid type."; // Output: "myFile is a valid type."
}
else
{
std::cout << filePath.filename() << " is an invalid type."; // Output: e.g. "myFile.cfg is an invalid type"
}
}
Run Code Online (Sandbox Code Playgroud)
另请参阅std :: filesystem :: path :: stem,std :: filesystem :: path :: filename。
小智 5
我今天偶然发现了这个问题,即使我已经有了一个有效的代码我发现它在某些情况下不起作用.
虽然有些人已经建议使用一些外部库,但我更喜欢编写自己的代码用于学习目的.
一些答案包括我首先使用的方法(寻找最后一个"."),但我记得在linux上隐藏的文件/文件夹以"."开头.因此,如果文件文件被隐藏且没有扩展名,则将使用整个文件名进行扩展.为了避免这种情况,我写了这段代码:
bool getFileExtension(const char * dir_separator, const std::string & file, std::string & ext)
{
std::size_t ext_pos = file.rfind(".");
std::size_t dir_pos = file.rfind(dir_separator);
if(ext_pos>dir_pos+1)
{
ext.append(file.begin()+ext_pos,file.end());
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
我没有完全测试过,但我认为它应该可行.