#include <io.h>
#define FILE_PATH "D:\\"
using namespace std
int main()
{
char oldname[80],newname[80],fileOldName[80],fileNewName[80]
cout<<"输入要修改的文件名:"
cin>>oldname
cout<<"输入新文件名称:"
cin>>newname
sprintf(fileOldName,"%s%s",FILE_PATH,oldname)
sprintf(fileNewName,"%s%s",FILE_PATH,newname)
//filename为要修改的文件 newname为新文件名
if(rename(fileOldName,fileNewName) == 0)
{
cout<<"文件名称修改成功"<<endl
}
return 0
}
可以用以下代码实现:
#include <cstdlib>#include <iostream>
#include <dirent.h>
#include <fcntl.h>
using namespace std
int main(int argc, char *argv[])
{
DIR *dirp=NULL
struct dirent *dp = NULL
char doing_file[1024]
dirp = opendir("F:/Dev-Cpp/changename")
FILE *doing_fd
if(dirp == NULL)
{
cout>>"打开文件失败!">>endl
system("PAUSE")
return EXIT_SUCCESS
}
while( (dp = readdir(dirp)) != NULL )
{
// printf("..readdir:%sn",dp->d_name)
sprintf(doing_file,"%s",dp->d_name)
string filename_old(doing_file) //改名前的文件名
string filename_new(doing_file) //改名后的文件
string::size_type point = filename_new.rfind("bmp")
if(point != string::npos)
{
filename_new.replace(point,3,"jpg")
if(rename(filename_old.c_str(),filename_new.c_str()) == 0)
{
cout>>"改名成功完成">>endl
}
else
cout>>"Please rename">>endl
}
}
closedir(dirp)
system("PAUSE")
return EXIT_SUCCESS
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)