假如给你一个文件路径,你可以将其中的文件名和文件后缀提取出来嘛? 不会没有关系,此篇博客提供一个函数,即可在Windows环境下调用,也可以在Linux环境下调用,去获取一个文件路径的文件名和文件后缀!
一、返回值 bool 版
需要包含头文件: #include <stdio.h> #include <string.h> #include <string>
代码如下:
bool get_fileName_fileSuffix(const char *_filePaht, std::string &_fileNames, std::string &_fileName, std::string &_fileSuffix, bool _dot = true) {
if (!_filePaht) return false;
std::string str = _filePaht;
if (str.empty()) return false;
if (str.at(0) == '.') return false;
if (str.at(str.size() - 1) == '.') return false;
char c = '\0';
#if defined (_WIN64) || defined (WIN32) || defined (_WIN32)
c = '\\';
#else
c = '\/';
#endif
std::string filename = strrchr(_filePaht, c) == NULL ? "" : strrchr(_filePaht, c) + 1;
if (filename == "") {
filename = _filePaht;
} else {
}
size_t _size = filename.rfind(".");
_fileName = filename.substr(0, _size);
std::string strsuffix = "";
if (_size != -1) {
if (_dot == true) {
strsuffix = filename.substr(_size);
} else {
strsuffix = filename.substr(_size + 1);
}
}
_fileSuffix = strsuffix;
_fileNames = filename;
return true;
}
具体过程就不解释了,都有详细注释,去看,去理解吧。
-
测试Windows路径: C:\abc\file.txt C:\abc\file file.txt file int main(void) {
std::string fileNames = "";
std::string fileName = "";
std::string fileSuffix = "";
bool suffix = true;
bool flag = false;
flag = get_fileName_fileSuffix("C:\\abc\\file.txt", fileNames, fileName, fileSuffix, suffix);
printf("str = %s, fileNames = %s fileName = %s fileSuffix = %s flag = %d\n\n", "C:\\abc\\file.txt", fileNames.c_str(), fileName.c_str(), fileSuffix.c_str(), flag);
flag = get_fileName_fileSuffix("C:\\abc\\file", fileNames, fileName, fileSuffix, suffix);
printf("str = %s, fileNames = %s fileName = %s fileSuffix = %s flag = %d\n\n", "C:\\abc\\file", fileNames.c_str(), fileName.c_str(), fileSuffix.c_str(), flag);
flag = get_fileName_fileSuffix("file.txt", fileNames, fileName, fileSuffix, suffix);
printf("str = %s, fileNames = %s fileName = %s fileSuffix = %s flag = %d\n\n", "file.txt", fileNames.c_str(), fileName.c_str(), fileSuffix.c_str(), flag);
flag = get_fileName_fileSuffix("file", fileNames, fileName, fileSuffix, suffix);
printf("str = %s, fileNames = %s fileName = %s fileSuffix = %s flag = %d\n\n", "file", fileNames.c_str(), fileName.c_str(), fileSuffix.c_str(), flag);
fileNames = "";
fileName = "";
fileSuffix = "";
flag = get_fileName_fileSuffix("", fileNames, fileName, fileSuffix, suffix);
printf("str = %s, fileNames = %s fileName = %s fileSuffix = %s flag = %d\n\n", "\"\"", fileNames.c_str(), fileName.c_str(), fileSuffix.c_str(), flag);
return 0;
}
运行截图: 这个是后缀带点(.txt)的测试 -
测试Linux路径: /abc/file.txt /abc/file file.txt file int main(void) {
std::string fileNames = "";
std::string fileName = "";
std::string fileSuffix = "";
bool suffix = false;
bool flag = false;
flag = get_fileName_fileSuffix("/abc/file.txt", fileNames, fileName, fileSuffix, suffix);
printf("str = %s, fileNames = %s fileName = %s fileSuffix = %s flag = %d\n\n", "/abc/file.txt", fileNames.c_str(), fileName.c_str(), fileSuffix.c_str(), flag);
flag = get_fileName_fileSuffix("/abc/file", fileNames, fileName, fileSuffix, suffix);
printf("str = %s, fileNames = %s fileName = %s fileSuffix = %s flag = %d\n\n", "/abc/file", fileNames.c_str(), fileName.c_str(), fileSuffix.c_str(), flag);
flag = get_fileName_fileSuffix("file.txt", fileNames, fileName, fileSuffix, suffix);
printf("str = %s, fileNames = %s fileName = %s fileSuffix = %s flag = %d\n\n", "/file.txt", fileNames.c_str(), fileName.c_str(), fileSuffix.c_str(), flag);
flag = get_fileName_fileSuffix("file", fileNames, fileName, fileSuffix, suffix);
printf("str = %s, fileNames = %s fileName = %s fileSuffix = %s flag = %d\n\n", "/file", fileNames.c_str(), fileName.c_str(), fileSuffix.c_str(), flag);
return 0;
}
运行截图: 这个是后缀不带点(txt)的测试
二、返回值 后缀索引 版
第一个版本是两个星期前写好的,后续回来看后,觉得返回值好像不是太友好,于是乎更改了以下版本代码。 返回的是 后缀索引 。
处理过程不变!
需要包含头文件: #include <stdio.h> #include <string.h> #include <string>
int get_fileName_fileSuffix(const char *_filePaht, std::string &_fullFileName, std::string &_fileName, std::string &_fileSuffix, bool _dot = true) {
if (!_filePaht) return -1;
std::string str = _filePaht;
if (str.empty()) return -1;
if (str.at(0) == '.') return -1;
if (str.at(str.size() - 1) == '.') return -1;
char c = '\0';
#if defined (_WIN64) || defined (WIN32) || defined (_WIN32)
c = '\\';
#else
c = '\/';
#endif
std::string filename = strrchr(_filePaht, c) == NULL ? "" : strrchr(_filePaht, c) + 1;
if (filename == "") {
filename = _filePaht;
} else {
}
size_t _size = filename.rfind(".");
_fileName = filename.substr(0, _size);
std::string strsuffix = "";
if (_size != -1) {
if (_dot == true) {
strsuffix = filename.substr(_size);
} else {
strsuffix = filename.substr(_size + 1);
}
}
_fileSuffix = strsuffix;
_fullFileName = filename;
return _size;
}
测试:
int main(void) {
std::string str = "abc.jpg";
std::string fullFilename, filename, fileSuffix;
int r = get_fileName_fileSuffix(str.c_str(), fullFilename, filename, fileSuffix, true);
printf("r = %d\n", r);
printf("fullFilename = %s filename = %s fileSuffix = %s\n", fullFilename.c_str(), filename.c_str(), fileSuffix.c_str());
return 0;
}
运行截图:
总结
本人在做项目时经常需要在一个路径中提取出文件名和文件路径,但是在项目中写的是零散的代码,所以在这里总结成一个函数,通过这个函数去获取就方便多了,而且windows和linux通用!(废了两个三个小时写代码、测试和写博客,值得!)
|