VC中读写INI文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VC中读写INI文件相关的知识,希望对你有一定的参考价值。

在VC2015中读写INI文件,文件以ANSI格式保存,如果以UTF-8保存,可能会产生乱码。

	LPCTSTR  strfile = _T(".//config.ini");
	TCHAR value[255] = { 0 };
	//读键值
	GetPrivateProfileString(
		_T("ui"),
		_T("button1"), 
		_T("default"),
		value,
		200,
		strfile);

	//写键值对
	WritePrivateProfileString(_T("UI"), _T("button1"), _T("启动"), strfile);
	
	//读整数
	int left = GetPrivateProfileInt(_T("UI"), _T("left"), 0, strfile);
	CString strleft;
	strleft.Format(_T("%d"),left);
	
	//读出某节的所有键值对
	TCHAR  chSection[1000];
	GetPrivateProfileSection(_T("UI"), chSection, 200, strfile);
	CStringArray list;
	int len;
	TCHAR *pBuf = chSection;
	while ((len = wcslen(pBuf)) > 0)
	{
		list.Add(pBuf);
		pBuf += len + 1;
	}

	//读出某节的所有键名
	TCHAR  chSectionName[1000];
	GetPrivateProfileSectionNames(chSectionName, 200, strfile);
	CStringArray list1;
	int len1;
	TCHAR *pBuf1 = chSectionName;
	while ((len1 = wcslen(pBuf1)) > 0)
	{
		list1.Add(pBuf1);
		pBuf1 += len1 + 1;
	}


以上是关于VC中读写INI文件的主要内容,如果未能解决你的问题,请参考以下文章

vc++读写配置文件

C/C++ 关于如何读写ini配置文件 (小结)

vb读写ini文件

python读写修改配置文件(ini)

vc的txt文件的读写操作

C#中读写INI文件