#include <iostream>
#include <io.h>
#include <xstring>
#include <vector>
#include <sstream>
#include <fstream>
#include <windows.h>
#include <direct.h>
using namespace std;
void getFiles(string path, vector<string>& files)
{
//文件句柄:文件的唯一标识
//long hFile = 0;//32位能跑动
intptr_t hFile = 0;//因为_findfirst和_findnext返回intptr_t而非long型
//定义存储文件信息的结构体
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)//string转化为c_str即C类型的字符串
{
do
{
//如果是目录,迭代之
//如果不是,加入列表
if ((fileinfo.attrib & _A_SUBDIR))//&:attrib和_A_SUBDIR做一次与运算,返回1则为_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 removeSuffix(string fileName)
{
const char* full_name = fileName.c_str();
const char* mn_first = full_name;
int tmp = strlen(full_name);//长度不包含'/0'
const char* mn_last = full_name /*+ strlen(full_name)*/;
if (strrchr(full_name, '\\') != NULL)//去除路径,只留文件名
mn_first = strrchr(full_name, '\\') + 1;
else if (strrchr(full_name, '/') != NULL)//去除linux下的文件路径
mn_first = strrchr(full_name, '/') + 1;
if (strrchr(full_name, '.') != NULL)
mn_last = strrchr(full_name, '.');//获取.*后缀
if (mn_last < mn_first)//地址比较
mn_last = full_name + strlen(full_name);
fileName.assign(mn_first, mn_last);//参数10001.jpg和.jpg
return fileName;
}
————————————————
版权声明:本文为CSDN博主「vict_wang」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/vict_wang/article/details/84033585