解决VC++ MFC程序resource.h头文件中ID重复问题

一般MFC开发的时候,如果有些资源是从其他工程中移植到本工程的,

而在资源移植的时候都要将对应的资源ID复制到本工程的resource.h文件中。

此时不管你在不在本工程中添加资源ID,resource.h文件中的ID可能都有重复的,一般再添加资源ID后,肯定有重复的。

可用下列代码解决此问题,将代码复制到空的win32控制台应用程序,将ressource.h拖入编译后产生可执行文件,即可解决此问题。

#include <iostream>
#include <fstream>
#include <string>
#include <list>
using namespace std;
typedef struct {
	string line_header;
	string line_id_name;
	int line_id_value;
	bool ishex;//value值是16进制?
	bool isdef;//是id定义字符串?
}FILE_ONE_LINE;
void handle_error(char * err)
{
	cout<<err<<endl;
	system("pause");
	exit(-1);
}
bool chisblank(char ch)
{
	return ch == '\t' || 
		ch == '\r' || 
		ch == '\n' || 
		ch == 32/*空格*/ ||
		ch == '\0';
}
int main(int argc,char * argv[])
{
	char * pfname=NULL;
	if(argc>=2)
		pfname = argv[1];
	else
		pfname = "resource.h";
	ifstream is(pfname);
	if(!is)
	{
		handle_error("ifstream:open file failed");
	}
	///文件读开始处理
	list<FILE_ONE_LINE> l;
	char temp[2000];
	char str[200];
	while(!is.eof())
	{
		is.getline(temp,2000);
		FILE_ONE_LINE line;
		line.ishex = false;
		if( temp[0] == '#' && 
			temp[1] == 'd' && 
			temp[2] == 'e' && 
			temp[3] == 'f' &&  
			temp[4] == 'i' &&  
			temp[5] == 'n' && 
			temp[6] == 'e' )
		{
			line.isdef = true;
			char * p=temp;
			int seg = 0;
			while(1)
			{
				while(chisblank(*p)&&( *p == 32 ||*p == '\t'))
				{
					p++;
				}
				int i=0;
				for(;!chisblank(*p);i++,p++)
				{
					str[i]=*p;
				}
				if(seg == 0)
				{
					str[i]='\0';
					line.line_header = str;
				}
				else if(seg == 1)
				{
					for(;i<40;i++)
						str[i]=' ';
					str[i]='\0';
					line.line_id_name = str;
				}
				else if(seg == 2)
				{
					str[i]='\0';
					if(str[0] == '0' && str[1]=='x')
					{
						line.ishex = true;
						sscanf(str,"%x",&line.line_id_value);
					}
					else
						line.line_id_value = atoi(str);
				}
				seg++;
				if(*p == '\n' || *p == '\0'|| seg >2)
					break;
			}
			l.push_back(line);
			//cout<<line.line_header<<'\t'<<line.line_id_name<<'\t'<<line.line_id_value<<endl;
		}
		else
		{
			line.line_header = temp;
			line.isdef = false;
			l.push_back(line);
			//cout<<line.line_header<<endl;
		}

	}
	if(is)
		is.close();
	///文件读处理完毕

	int index=-1;
	list<FILE_ONE_LINE>::iterator it = l.begin();

	for(;it != l.end();it ++)
	{
		if(index<0)
			index = it ->line_id_value; 
		if(it ->line_id_value >= 100 && index < 100)
			index = it ->line_id_value;
		if(it ->line_id_value >= 1000 && index < 1000)
			index = it ->line_id_value;
		if(it ->line_id_value >= 30000 && index < 30000)
			index = it ->line_id_value;
		if(it ->isdef)
			it ->line_id_value = index++;
	}
	///文件写开始
	ofstream os(pfname);
	if(!os)
	{
		is.close();
		handle_error("ostream:Open file failed");
	}
	for(it = l.begin();it != l.end();it ++)
	{
		if(it ->isdef)
		{
			if(!it ->ishex)
				sprintf(temp,"%d",it ->line_id_value);
			else
				sprintf(temp,"0x%04x",it ->line_id_value);
			os<<it ->line_header<<" "<<it ->line_id_name<<" "<<temp<<'\n';
		}
		else
		{
			os<<it ->line_header<<'\n';
		}
	}

	if(os)
		os.close();
	///文件写处理完毕
	l.clear();
	cout<<"changed!OK!"<<endl;
	system("pause");
	return 0;
}


 

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值