rename函数的使用

在工程中遇到一个问题,需要把整个文件夹中的图片名称命名为连续数字命名的图片。比如原来图片名称是re.jpg,name.jpg,但是我现在要改为1.jpg和2.jpg。这里要使用一个叫rename的函数。

当我把这个文件夹中的名称重新命名的时候,发现如果只在一个文件夹中使用的话,会把已经重新命名的图片再次命名。(不知道为啥,求指点)最好是重新rename到其他文件夹中,rename有移动文件的效果喔。


关于代码的一点解释:文件夹中有不同的图片,但是相同的序号的内容是一样,有灰度和二值的区别,如下:


#include "stdafx.h"
#include "afx.h"   //使用CString,添加此库函数的同时,修改:在静态库中使用 MFC,使用多字节字符集

#include <opencv2/opencv.hpp>
#include "io.h"
#include <windows.h>
//#include <cstdio>


using namespace std;




int main(int argc, char* argv[])
{
	//要读取的文件夹路径
	string file_path("F:\\项目\\1221273\\1221273\\target1\\");//想要修改的文件夹的路径
	string newFilePath("C:\\Users\\GUOXJ\\Desktop\\newtest\\");  //重新写到这个文件夹中
	//存储文件信息
	struct _finddata_t file_info;
	//文件的句柄
	intptr_t  file_handle;

	string fullPath = file_path + "*.jpg";
	file_handle = _findfirst(fullPath.c_str(), &file_info);


	//读取文件夹下文件,判断.jpg是否存在
	if (file_handle == -1)
	{
		printf("没有找到.jpg文件!\n");
		return 0;
	}
	

	int flag = 0;
	int index = 0;
	char t_str1[10];
	_itoa_s(index,t_str1,10);				//将i转换为图像命名的一部分
	string str(t_str1);
	string sNewName1 =  newFilePath + str + "Nuc.jpg";		//灰度图
	string sNewName2 = newFilePath + str + "NucEdge.jpg";   //二值图
	string oldname1 = file_path + file_info.name;
	rename(oldname1.c_str(),sNewName1.c_str());

	    _findnext(file_handle, &file_info);

		string oldname2 = file_path + file_info.name;
		rename(oldname2.c_str(),sNewName2.c_str());



	while(_findnext(file_handle, &file_info) == 0)
	{
		if ( flag == 0 )
		{
			index++;
			_itoa_s(index,t_str1,10);				//将i转换为图像命名的一部分
			string str(t_str1);
			string sNewName1 =  newFilePath + str + "Nuc.jpg";		//灰度图
			string oldname1 = file_path + file_info.name;
			rename(oldname1.c_str(),sNewName1.c_str());

		}
		else
		{
			_itoa_s(index,t_str1,10);				//将i转换为图像命名的一部分
			string str(t_str1);
			string sNewName2 = newFilePath + str + "NucEdge.jpg";   //二值图
			string oldname2 = file_path + file_info.name;
			rename(oldname2.c_str(),sNewName2.c_str());

		}

	   flag = 1 - flag;

	}

		_findclose(file_handle);

	//system("pause");
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值