OSG C++ 获取文件后缀 OSG修改文件后缀名:
在使用OSG开发时,可以使用OSG提供的接口快速实现文件后缀名的修改,而不用麻烦地自己去写C++代码。
在osg的osgDB/FileNameUtils库中,提供了获取和修改文件后缀的函数,如下:
/** Gets the parent path from full name (Ex: /a/b/c.Ext => /a/b). */
extern OSGDB_EXPORT std::string getFilePath(const std::string& filename);
/** Gets the extension without dot (Ex: /a/b/c.Ext => Ext). */
extern OSGDB_EXPORT std::string getFileExtension(const std::string& filename);
/** Gets the extension including dot (Ex: /a/b/c.Ext => .Ext). */
extern OSGDB_EXPORT std::string getFileExtensionIncludingDot(const std::string& filename);
/** Gets the lowercase extension without dot (Ex: /a/b/c.Ext => ext). */
extern OSGDB_EXPORT std::string getLowerCaseFileExtension(const std::string& filename);
/** Gets file name with extension (Ex: /a/b/c.Ext => c.Ext). */
extern OSGDB_EXPORT std::string getSimpleFileName(const std::string& fileName);
/** Gets file path without last extension (Ex: /a/b/c.Ext => /a/b/c ; file.ext1.ext2 => file.ext1). */
extern OSGDB_EXPORT std::string getNameLessExtension(const std::string& fileName);
/** Gets file path without \b all extensions (Ex: /a/b/c.Ext => /a/b/c ; file.ext1.ext2 => file). */
extern OSGDB_EXPORT std::string getNameLessAllExtensions(const std::string& fileName);
/** Gets file name without last extension (Ex: /a/b/c.Ext => c ; file.ext1.ext2 => file.ext1). */
extern OSGDB_EXPORT std::string getStrippedName(const std::string& fileName);
如何修改文件后缀名呢,可以参考下面的代码:
首先引入头文件:
#include <osgDB/FileNameUtils>
然后给定一个目录文件,只需要两句代码即可修改后缀名,如下:
string fileName = "D:\\all.txt";
std::cout << osgDB::getNameLessExtension(fileName); // D:\\all
string newfileName = osgDB::getNameLessExtension(fileName) + ".xml"; //D:\\all.xml