关于ini配置文件操作的api,推荐博文C++操作.ini配置文件的API
题目描述如下:
程序的配置信息一般存储在一个ini文件中,当程序运行时会从ini文件中读取一些启动信息。程序运行过程中也会对这些配置信息进行修改以保存本次程序运行的相关数据。请编写一个简单的配置数据操作类,主要包括以下功能:
(1)读取ini文件中指定键的值。
(2)修改ini文件中某个键的值。
(3)删除ini文件中的某个键。
(4)添加新的键到ini文件中。
其实直接用MFC实现即可,但是为了训练一下DLL的调用方式,还是用DLL实现1、利用vc++6.0新建DLL工程
lib.h
#define work2 _declspec(dllexport) extern "C" work2 bool readKey(char* section,char* key,char* value,char* path); extern "C" work2 bool changeKey(char* section,char* key,char* value,char* path); extern "C" work2 bool deleteKey(char* section,char* key,char* path); extern "C" work2 bool addKey(char* section,char* key,char* value,char* path);lib.cpp,里面用到的API都是在windows.h中定义的
#include"lib.h" bool addKey(char* section,char* key,char* value,char* path) { return WritePrivateProfileString(section,key,value,path);//向配置文件中写入记录,参数section表示区块,以键值对的形式存储,若果制定路径不包含该文件则创建文件,文件类型为ini } bool deleteKey(char* section,char* key,char* path) { return WritePrivateProfileString(section,key,NULL,path);//当value为NULL时,相当于删除该记录 } bool changeKey(char* section,char* key,char* value,char* path) { return WritePrivateProfileString(section,key,value,path); } bool readKey(char* section,char* key,char* value,char* path) { return GetPrivateProfileString("Section1",key,"",value,16,path);//从相应区块中获取键的值,存于value中 }
静态调用如下:
#define work2 _declspec(dllimport)//导入函数 #pragma comment(lib,"work2.lib"); extern "C" work2 bool readKey(char* section,char* key,char* value,char* path); extern "C" work2 bool changeKey(char* section,char* key,char* value,char* path); extern "C" work2 bool deleteKey(char* section,char* key,char* path); extern "C" work2 bool addKey(char* section,char* key,char* value,char* path);导入之后即可直接在MFC中使用这些函数了
界面布局如下:
功能实现:
void CWork2TestDlg::OnButton1() //添加配置信息 { // TODO: Add your control notification handler code here bool b = false; CString section,key,value; section = "Section1"; GetDlgItem(IDC_EDIT1) -> GetWindowText(section); GetDlgItem(IDC_EDIT2) -> GetWindowText(key); GetDlgItem(IDC_EDIT3) -> GetWindowText(value); b = addKey((LPSTR)(LPCTSTR)section,(LPSTR)(LPCTSTR)key,(LPSTR)(LPCTSTR)value,"./work.ini"); if(b == false) MessageBox("添加失败"); else MessageBox("添加成功"); } void CWork2TestDlg::OnButton3() //删除个顶区块的某个键值对 { // TODO: Add your control notification handler code here bool b = false; CString section,key; section = "Section1"; GetDlgItem(IDC_EDIT1) -> GetWindowText(section); GetDlgItem(IDC_EDIT2) -> GetWindowText(key); b = deleteKey((LPSTR)(LPCTSTR)section,(LPSTR)(LPCTSTR)key,"./work.ini"); if(b == false) MessageBox("删除失败"); else MessageBox("删除成功"); } void CWork2TestDlg::OnButton4() //修改 { // TODO: Add your control notification handler code here bool b = false; CString section,key,value; section = "Section1"; GetDlgItem(IDC_EDIT1) -> GetWindowText(section); b = addKey((LPSTR)(LPCTSTR)section,(LPSTR)(LPCTSTR)key,(LPSTR)(LPCTSTR)value,"./work.ini"); if(b == false) MessageBox("修改失败"); else MessageBox("修改成功"); } void CWork2TestDlg::OnButton2()//显示整个ini文件 { // TODO: Add your control notification handler code here string iniText = ""; ifstream fin("./work.ini"); string s; int i = 0; while(getline(fin,s))//按行读取配置文件,组成整个字符串,将显示文本框设置为多行显示,然后再每一行后面添加“\r\n”,就能显示整个文件,目前还没想到更好的办法 { iniText.append(s); iniText.append("\r\n");//添加换行标志 GetDlgItem(IDC_EDIT4) -> SetWindowText(iniText.c_str());//string转化为CString显示 } // GetDlgItem(IDC_EDIT4) -> SetWindowText(iniText); } void CWork2TestDlg::OnButton5() { // TODO: Add your control notification handler code here bool b = false; CString section,key,value=""; section = "Section1"; GetDlgItem(IDC_EDIT1) -> GetWindowText(section); GetDlgItem(IDC_EDIT2) -> GetWindowText(key); b = readKey((LPSTR)(LPCTSTR)section,(LPSTR)(LPCTSTR)key,(LPSTR)(LPCTSTR)value,"./work.ini"); key += "="; if(b == false) MessageBox("获取失败"); else MessageBox(value); }
SetDlgItemText(IDC_EDIT1,"Section1");