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;
	}














本文转自Chinayu201451CTO博客,原文链接:http://blog.51cto.com/9233403/2070396 ,如需转载请自行联系原作者

相关文章
|
24天前
【QT】读写.ini配置文件的程序实现
【QT】读写.ini配置文件的程序实现
|
7月前
|
存储 API Windows
QT读写.INI文件的实现方法
QT读写.INI文件的实现方法
118 0
|
10月前
|
存储 API 数据格式
Qt通过QSttings类读取*.ini配置文件
Qt通过QSttings类读取*.ini配置文件
155 0
|
存储 JavaScript Windows
autojs-读写ini
autojs-读写ini
244 0
Qt读写ini文件的C++类
Qt读写ini文件的C++类
160 0
|
API
INI文件的写入与读取
INI文件的写入与读取  [节名]         '[]中的节名对应此API的第一参数 Name=内容      'Nmae对应此API的第二参数 API的第三参数是没有取到匹配内容时返回的字符串; API的第四参数是要返回的字符串; API的第五参数是字符串缓冲的长度,一般255; API的第六参数是INI文件的路径。
1314 0