"C + +" read and write INI configuration file __c++

Source: Internet
Author: User


Gets the current path
	CString Szcurpath (_t (""));
	GetModuleFileName (Null,szcurpath.getbuffer (MAX_PATH), MAX_PATH);	
	Szcurpath.releasebuffer ();
	G_szcurpath = Szcurpath.left (Szcurpath.reversefind (' \ ") + 1);

	The settings file 
	G_szsettingpath = G_szcurpath + _t ("Setting.ini") under the current path;
	CFileFind find;
	if (!find. FindFile (G_szsettingpath)) Makeresource (Idr_setting,g_szsettingpath);
	Find. Close ();


BOOL Makeresource (int nID, CString szpathname)
{
	hrsrc hsrc = FindResource (Null,makeintresource (NID), _t (" Owner_data "));
	if (hsrc = NULL) return	false;

	Hglobal hglobal = LoadResource (NULL,HSRC);
	if (hglobal = NULL) return	false;
	
	LPVOID LP = Lockresource (HGLOBAL);
	DWORD dwsize = Sizeofresource (NULL,HSRC);

	CFile file;
	if (file. Open (szpathname,cfile::modecreate| Cfile::modewrite))
	{
		file. Write (lp,dwsize);
		File. Close ();
	}
	FreeResource (hglobal);

	return true;
}


CString szsection = _t ("Setting");
	CString szkey =_t ("Langnum");
	CString Szlang;
	DWORD dwsize = m;
	Nlangnum = M_cmblang.getcursel ();
	Szlang.format (_t ("%d"), nlangnum);
	WritePrivateProfileString (Szsection,szkey,szlang,g_szsettingpath);
	Szlang.releasebuffer ();



CString szsection = _t ("Setting");
	CString szkey =_t ("Langnum"), Szlang;
	DWORD dwsize = m;
	GetPrivateProfileString (szsection,szkey,_t (""), Szlang.getbuffer (dwsize), dwsize,g_szsettingpath);
	SWSCANF (szlang,_t ("%d"), &nlangnum);
	Szlang.releasebuffer ();


Double Cinifile::getinitvalue (LPCTSTR inifilename,lpctstr secname,lpctstr paraname)
{
	WCHAR valbuf[128];
	GetPrivateProfileString (secname,paraname,_t (""), valbuf,128,inifilename);
	Return _ttof (VALBUF);
}

Short Cinifile::setinitvalue (LPCTSTR inifilename,lpctstr secname,lpctstr paraname,double Para)
{
	CString Valstr;
	Valstr.format (_t ("%.3f"), Para);
	WritePrivateProfileString (secname,paraname,valstr,inifilename);
	return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.