Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <io.h>
- #include <string>
- #include <vector>
- using namespace std;
- /*
- * 功能:遍历文件和子文件夹内的文件
- * 参数:
- * 状态:
- */
- void getFiles(string path, vector<string>& files)
- {
- //文件句柄
- long hFile = 0;
- //文件信息
- struct _finddata_t fileinfo;
- string p;
- if ((hFile = _findfirst(p.assign(path).append("\\*.JPG").c_str(), &fileinfo)) != -1)
- {
- do
- {
- //如果是目录,迭代之,如果不是,加入列表
- if ((fileinfo.attrib &_A_SUBDIR))
- {
- if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
- getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
- }
- else
- {
- files.push_back(p.assign(path).append("\\").append(fileinfo.name));
- }
- } while (_findnext(hFile, &fileinfo) == 0);
- _findclose(hFile);
- }
- }
- /*
- * 功能:输入文件的全路径获得文件名不带扩展
- * 参数:
- * 状态:
- */
- string getFilename(string s)
- {
- char sep = '/';
- char sepExt = '.';
- #ifdef _WIN32
- sep = '\\';
- #endif
- size_t i = s.rfind(sep, s.length());
- if (i != string::npos) {
- string fn = (s.substr(i + 1, s.length() - i));
- size_t j = fn.rfind(sepExt, fn.length());
- if (i != string::npos) {
- return fn.substr(0, j);
- }
- else{
- return fn;
- }
- }
- else{
- return ""; //若没有找到则返回空值
- }
- }
- /*
- * 功能:输入文件的全路径获得带扩展的文件名
- * 参数:
- * 状态:
- */
- string getFilenameWithExt(string s)
- {
- char sep = '\\';
- size_t i = s.rfind(sep, s.length()); //找到字符‘\\’最后一次出现的位置
- if (i != string::npos){
- string fn = (s.substr(i + 1, s.length() - i)); //取得带扩展的文件名
- return fn;
- }
- }
- /*
- * 功能:输入文件的全路径获得文件名不带扩展
- * 参数:
- * 状态:
- */
- string getFilenameWithNoExt(string s)
- {
- char sep = '/';
- char sepExt = '.';
- #ifdef _WIN32
- sep = '\\';
- #endif
- size_t i = s.rfind(sep, s.length());
- if (i != string::npos) {
- string fn = (s.substr(i + 1, s.length() - i));
- size_t j = fn.rfind(sepExt, fn.length());
- if (i != string::npos) {
- return fn.substr(0, j);
- }
- else{
- return fn;
- }
- }
- else{
- return ""; //若没有找到则返回空值
- }
- }
- string getFilenameWithNoExt_m(string s)
- {
- char sep = '\\';
- char sepExt = '.';
- size_t i = s.rfind(sep, s.length()); //找到字符‘/’最后一次出现的位置
- if (i != string::npos)
- {
- string fn = (s.substr(i + 1, s.length() - i)); //取得带扩展的文件名
- size_t j = fn.rfind(sepExt, fn.length()); //找到‘.’最后一次出现的位置
- if (j != string::npos) {
- return fn.substr(0, j); //返回
- }
- else{
- return fn;
- }
- }
- else{
- size_t k = s.rfind(sepExt, s.length());
- return s.substr(0, k);
- }
- }
- /*
- * 功能:输入文件的全路径获得文件名不带扩展
- * 参数:
- * 状态:
- */
- static string getFilename(string s)
- {
- char sep = '/';
- char sepExt = '.';
- #ifdef _WIN32
- sep = '\\';
- #endif
- size_t i = s.rfind(sep, s.length());
- if (i != string::npos) {
- string fn = (s.substr(i + 1, s.length() - i));
- size_t j = fn.rfind(sepExt, fn.length());
- if (i != string::npos) {
- return fn.substr(0, j);
- }
- else{
- return fn;
- }
- }
- else{
- return ""; //若没有找到则返回空值
- }
- }
- /*
- * 功能:输入文件的全路径获得除扩展外的包含路径和文件名的字符串
- * 参数:
- * 状态:
- */
- static string getFilePath(std::string s)
- {
- //std::string s = replace_all(s1, "\\", "\\\\");
- char sepExt = '.';
- size_t i = s.rfind(sepExt, s.length());
- if (i != std::string::npos)
- {
- std::string path = s.substr(0, i);
- return path;
- }
- else
- {
- std::cout << s << "is not a file name" << std::endl;
- return "";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement