环境vs2013+opencv2.4.9+win10
c++代码
#include <iostream>
#include <sstream>//格式转换,这里是int转为string
#include <string>
#include <io.h> //进行系统文件操作的头文件
using namespace std;
const int N = 3;//假如少于100张,N=2,则编号1~99;N=3,则编号01~099,N=4,则编号0001~0099
const string fileType = ".png";//需要重命名图片的格式
string int2string(int n, int i);//类型转换函数
int main()
{
_finddata_t file; // 查找文件的类
string fileDirectory = "C:\\Users\\2105Share\\Desktop\\trafficSignImage"; //文件夹目录,自己修改
string buffer = fileDirectory + "\\*" + fileType;
//long hFile;//win7系统
intptr_t hFile;//win10系统
hFile = _findfirst(buffer.c_str(), &file); //找第一个文件
if (hFile == -1L)
cout << "没有指定格式的图片" << endl;//没有指定格式的图片
else
{
int i = 0;
string newFullFilePath;
string oldFullFilePath;
string strName;
do
{
oldFullFilePath.clear();
newFullFilePath.clear();
strName.clear();
oldFullFilePath = fileDirectory + "\\" + file.name;
++i;
strName = int2string(N, i); //类型转换
newFullFilePath = fileDirectory + "\\" + strName + fileType;
int c = rename(oldFullFilePath.c_str(), newFullFilePath.c_str());//重命名
if (c == 0)
cout << "重命名成功"<<strName<<fileType<<endl;
else
cout << "重命名失败" << strName << fileType << endl;
} while (_findnext(hFile, &file) == 0);
_findclose(hFile);
}
system("pause");
return 0;
}
string int2string(int n, int i)//类型转换函数
{
char s[BUFSIZ];
sprintf_s(s, "%d", i);
int len = strlen(s);
if (len > n)
{
cout << "输入的N太小!";
}
else
{
stringstream Num;
for (int i = 0; i < n - len; i++)
Num << "0";
Num << i;
return Num.str();
}
}
运行结果