C++ 配置文件读写

void CAamsProtocol::WriteConfig()
{
	TCHAR tcExePath[MAX_PATH] = {0};  
	::GetModuleFileName(NULL, tcExePath, MAX_PATH);  
	// 设置ini路径到exe同一目录下  
#ifndef CONFIG_FILE  
#define CONFIG_FILE     (TEXT("Config.ini"))  
#endif  
	//_tcsrchr() 反向搜索获得最后一个'\\'的位置,并返回该位置的指针  
	TCHAR *pFind = _tcsrchr(tcExePath, '\\');  
	if (pFind == NULL)  
	{  
		return;  
	}  
	*pFind = '\0';  

	CString szIniPath = tcExePath;  
	szIniPath += "\\";  
	szIniPath += CONFIG_FILE;  

	//--------------------------------------------------------  
	//BOOL WritePrivateProfileString(  
	//                              LPCTSTR lpAppName,      //节的名字,是一个以0结束的字符串  
	//                              LPCTSTR lpKeyName,      //键的名字,是一个以0结束的字符串。若为NULL,则删除整个节  
	//                              LPCTSTR lpString,       //键的值,是一个以0结束的字符串。若为NULL,则删除对应的键  
	//                              LPCTSTR lpFileName      //要写入的文件的文件名。若该ini文件与程序在同一个目录下,  
	//                              )                          也可使用相对路径,否则需要给出绝度路径。  
	//如果Ini不存在,它会自动在szIniPath上创建此INI文件.再执行写入.   
	CString ip1;
	ip1.Format(_T("%d"),(work.nSocketAddress & 0xFF000000) >> 24);
	CString ip2;
	ip2.Format(_T("%d"),(work.nSocketAddress & 0x00FF0000) >> 16);
	CString ip3;
	ip3.Format(_T("%d"),(work.nSocketAddress & 0x0000FF00) >> 8);
	CString ip4;
	ip4.Format(_T("%d"),(work.nSocketAddress & 0x000000FF) );
	::WritePrivateProfileString("Login", "ip1", ip1, szIniPath); 
	::WritePrivateProfileString("Login", "ip2", ip2, szIniPath); 
	::WritePrivateProfileString("Login", "ip3", ip3, szIniPath); 
	::WritePrivateProfileString("Login", "ip4", ip4, szIniPath); 
	CString port;
	port.Format(_T("%d"),work.nHostPort);
	::WritePrivateProfileString("Login", "port", port, szIniPath); 
	CString type;
	type.Format(_T("%d"),(int)work.type);
	::WritePrivateProfileString("Login", "worktype", type, szIniPath); 
	::WritePrivateProfileString("Login", "site", work.szSite, szIniPath);  
	::WritePrivateProfileString("Login", "user", work.szUser, szIniPath); 
}


void CAamsProtocol::ReadConfig()
{
	TCHAR tcExePath[MAX_PATH] = {0};  
	::GetModuleFileName(NULL, tcExePath, MAX_PATH);  
	// 设置ini路径到exe同一目录下  
#ifndef CONFIG_FILE  
#define CONFIG_FILE     (TEXT("Config.ini"))  
#endif  
	//_tcsrchr() 反向搜索获得最后一个'\\'的位置,并返回该位置的指针  
	TCHAR *pFind = _tcsrchr(tcExePath, '\\');  
	if (pFind == NULL)  
	{  
		return;  
	}  
	*pFind = '\0';  

	CString szIniPath = tcExePath;  
	szIniPath += "\\";  
	szIniPath += CONFIG_FILE; 

	if (!::PathFileExists(szIniPath))  
	{  
		AfxMessageBox(_T("配置文件不存在!"));
		return;  
	}  

	int ip1 = ::GetPrivateProfileInt("Login", "ip1", 192, szIniPath);
	int ip2 = ::GetPrivateProfileInt("Login", "ip2", 168, szIniPath);
	int ip3 = ::GetPrivateProfileInt("Login", "ip3", 1, szIniPath);
	int ip4 = ::GetPrivateProfileInt("Login", "ip4", 42, szIniPath);
	work.nSocketAddress = ((ip1<<24) & 0xFF000000)+((ip2<<16) & 0x00FF0000)+((ip3<<8) & 0x0000FF00)+(ip4 & 0x000000FF);
	work.nHostPort = ::GetPrivateProfileInt("Login", "port", 6000, szIniPath);
	work.type = (EWorkType)::GetPrivateProfileInt("Login", "worktype", 0, szIniPath);
	::GetPrivateProfileString("Login", "site", "1", work.szSite, MAX_PATH, szIniPath);  
	::GetPrivateProfileString("Login", "user", "admin", work.szUser, MAX_PATH, szIniPath);
	::GetPrivateProfileString("Login", "password", "admin", work.szPassword, MAX_PATH, szIniPath);

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值