VC ini配置文件常用操作

A.读写ini文件

ini文件(即Initialization file),这种类型的文件中通常存放的是一个程序的初始化信息。ini文件由若干个节(Section)组成,每个Section由若干键(Key)组成,每个Key可以赋相应的值。读写ini文件实际上就是读写某个的Section中相应的Key的值,而这只要借助几个函数即可完成。

写函数(一个):

BOOL WritePrivateProfileString();//写字符串

读函数(两个):

DWORD GetPrivateProfileString();//读字符串数据
UINT GetPrivateProfileInt();//读整型数据

如:

[Student]	//Section,节
Name = yu	//Key,键
Age = 25	// Key,键

#写函数只有一个,所以int数据要先转换成CString,然后再写。

#配置文件没有修改函数,如果要修改某个key的值,那么直接往该key中写入即可

#如果 ini中没有指定的 SectionAPI会新建 Section,如果没有指定的 Key则新建一个 Key 并写入数据,如果已经存在,则用字符串代替原来的值。当指定的 ini也不存在的时候,API会自动建立一个新的文件

 

写配置文件:

{
	CString StrName,StrAge;//姓名,年龄
	int nAge;

	StrName = _T("yu");
	nAge = 25;
	StrAge.Format(_T("%d"),nAge);//将int数据转换为CString,然后写入

	//WritePrivateProfileString(Section名,Key名,CString数据,文件地址);
	::WritePrivateProfileString(_T("Student"),_T("Name"),StrName,_T(".\\setting.ini"));//写入姓名
	::WritePrivateProfileString(_T("Student"),_T("Age"),StrAge,_T(".\\setting.ini"));//写入年龄
}

读配置文件:

{
	CFileFind finder;//定义标准类对象
	BOOL isFind;
	isFind = finder.FindFile(_T(".\\setting.ini"));//是否查找到文件(内为文件路径)
	if (isFind)
	{
		CString strName, strAge;
		int age;
	
		//读取名字
		::GetPrivateProfileString(_T("Student"), _T("Name"), _T("NULL"), strName.GetBufferSetLength(15), 15, _T(".\\setting.ini"));
		//Section名,Key名,默认值,存储字符串,字符串所允许的最大长度(15),文件路径
		strName.ReleaseBuffer();//此操作对应于GetBufferSetLength(),紧跟其后,不能忽略。此操作的作用是将GetBufferSetLength()申请的多余的内存空间释放掉,以便于可以进行后续的如字符串+操作
		m_edit_name.SetWindowText(strName);
	
		//读取年龄
		age = ::GetPrivateProfileInt(_T("Student"), _T("Age"), 0, _T(".\\setting.ini"));
		//Section名,Key名,默认值,文件路径
		strAge.Format(_T("%d"), age); //将int型数据转换为CString类型数据
		m_edit_age.SetWindowText(strAge);
	
		UpdateData(FALSE);
	}
}

三个函数的原型:

写函数(一个)

BOOL WritePrivateProfileString(//写字符串
	LPCTSTR lpAppName,  // pointer to section name
	LPCTSTR lpKeyName,  // pointer to key name.若为NULL,删除整个Section
	LPCTSTR lpString,   // key的具体内容。若为NULL,删除整个Key
	LPCTSTR lpFileName  // ini文件所在路径
);

读函数 ( 两个 )

DWORD GetPrivateProfileString(//读字符串数据
	LPCTSTR lpAppName,        // points to section name
	LPCTSTR lpKeyName,        // points to key name
	LPCTSTR lpDefault,        // 默认返回值,读取失败时返回
	LPTSTR  lpReturnedString, // 字符串缓冲区,读取的数据存储在这里
	DWORD   nSize,            // 缓冲区最大值
	LPCTSTR lpFileName        // ini文件所在路径
);
UINT GetPrivateProfileInt(//读整型数据
	LPCTSTR lpAppName,  // address of section name
	LPCTSTR lpKeyName,  // address of key name
	INT	 nDefault,    // 默认返回值,读取失败时返回
	LPCTSTR lpFileName  // ini文件所在路径
);

B .删除 Section Key

注意写函数:

BOOL WritePrivateProfileString(//写字符串
	LPCTSTR lpAppName,  // pointer to section name
	LPCTSTR lpKeyName,  // pointer to key name.若为NULL,删除整个Section
	LPCTSTR lpString,   // key的具体内容。若为NULL,删除整个Key
	LPCTSTR lpFileName  // ini文件所在路径
);
故如果要:

删除某个Section将函数第二个和第三个参数设置为NULL

::WritePrivateProfileString(_T("Student"),NULL,NULL,_T(".\\setting.ini"));

删除某个Key将函数第三个参数设置为NULL

::WritePrivateProfileString(_T("Student"),_T("Name"),NULL,_T(".\\setting.ini"));

 

C.获取ini文件中所有的Section

使用GetPrivateProfileSectionNames()函数来获取所有的Section名。

DWORD GetPrivateProfileSectionNames(
	LPTSTR lpszReturnBuffer,  // address of return buffer
	DWORD nSize,              // size of return buffer
	LPCTSTR lpFileName        // address of initialization filename
);
函数返回值为读入缓冲区的字符数。

该函数用于从ini文件中获取所有的Section名。当不知道ini中有哪些Section的时候使用该api函数。

如ini中有两个Section: [sec1]和[sec2],则返回的是:  'sec1 ',0, 'sec2',0,0

即两个Section名之间有符号’0’。最后结尾也是符号’0’。所以最后会有两个连续的符号’0’。

可以使用该函数来求Section的个数。其原理是:将所有的Section名都找出来,然后统计节名的个数。

 

D.所有的ini文件函数

APIINI文件的函数有

BOOL WritePrivateProfileString(

	LPCTSTR lpAppName,	// 节名
	LPCTSTR lpKeyName,	// 键名 
	LPCTSTR lpString,	// 添加的字符串 
	LPCTSTR lpFileName  // Ini文件名 
);

BOOL WritePrivateProfileStruct(
	LPCTSTR lpszSection,	// pointer to section name
	LPCTSTR lpszKey,		// pointer to key name
	LPVOID lpStruct,		// 要写入的数据缓冲区
	UINT uSizeStruct,		// 缓冲区的大小
	LPCTSTR szFile			// pointer to initialization filename
);
BOOL WritePrivateProfileSection(

	LPCTSTR lpAppName,	// pointer to string with section name 
	LPCTSTR lpString,	// 写入的字符串
	LPCTSTR lpFileName  // pointer to string with filename 
);
API INI 文件的函数有
DWORD GetPrivateProfileString(

	LPCTSTR lpAppName, // points to section name 
	LPCTSTR lpKeyName, // points to key name 
	LPCTSTR lpDefault, // 默认字符串 ,如果没有则返回该值
	LPTSTR lpReturnedString, // 返回的字符串 
	DWORD nSize, // 返回字符串的大小 
	LPCTSTR lpFileName  // points to initialization filename 
);
DWORD GetPrivateProfileSection(

	LPCTSTR lpAppName, // address of section name 
	LPTSTR lpReturnedString, // address of return buffer 
	DWORD nSize, // size of return buffer 
	LPCTSTR lpFileName  // address of initialization filename  
);
UINT GetPrivateProfileInt(

	LPCTSTR lpAppName, // address of section name
	LPCTSTR lpKeyName, // address of key name
	INT nDefault, // return value if key name is not found
	LPCTSTR lpFileName  // address of initialization filename
);
BOOL GetPrivateProfileStruct(

	LPCTSTR lpszSection, // address of section name
	LPCTSTR lpszKey, // address of key name
	LPVOID lpStruct, // address of return buffer
	UINT uSizeStruct, // size of return buffer
	LPCTSTR szFile // address of initialization filename
);
DWORD GetPrivateProfileSectionNames(

	LPTSTR lpszReturnBuffer, // address of return buffer 
	DWORD nSize, // size of return buffer 
	LPCTSTR lpFileName // address of initialization filename
);


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值