重命名之后
C++
#include <iostream>
#include <io.h>
#include <string>
#include <sstream>
#include<vector>
using namespace std;
const int N = 6;
const string FileType = ".jpg";
string int2string(int n, int i)
{
char s[BUFSIZ];
sprintf(s, "%d", i);
int l = strlen(s);
if (l > n)
{
cout << "整型的长度大于需要格式化的字符串长度!";
}
else
{
stringstream M_num;
for (int i = 0;i < n - l;i++)
M_num << "0";
M_num << i;
return M_num.str();
}
}
int main()
{
_finddata_t c_file;
string File_Directory ="E:\\image";
string buffer = File_Directory + "\\*" + FileType;
intptr_t hFile;
hFile = _findfirst(buffer.c_str(), &c_file);
if (hFile == -1L)
printf("No %s files in current directory!\n", FileType);
else
{
printf("Listing of files:\n");
int i = 0;
string newfullFilePath;
string oldfullFilePath;
string str_name;
do
{
oldfullFilePath.clear();
newfullFilePath.clear();
str_name.clear();
oldfullFilePath = File_Directory + "\\" + c_file.name;
++i;
str_name = int2string(N, i);
newfullFilePath = File_Directory + "\\"+ str_name + FileType;
int c = rename(oldfullFilePath.c_str(), newfullFilePath.c_str());
if (c == 0)
puts("File successfully renamed");
else
perror("Error renaming file");
} while (_findnext(hFile, &c_file) == 0);
_findclose(hFile);
}
return 0;
}
原文件夹