C++ INI文件
来源:互联网 发布:手机勒索软件生成 编辑:程序博客网 时间:2024/06/07 07:26
INI文件多用于存储程序的初始化信息。例如,记录程序连接数据库的名称、上一次用户登录的名称、用户的注册信息等。
一. INI 文件格式
[Section1 Name]KeyName1=value1KeyName2=value2...... ......[Section2 Name]KeyName1=value1KeyName2=value2
Section:节名;KeyName:键名;value:键值。对于一个INI文件,可以有多个节,每个节可以包含多个键。
二.读写INI文件
1. GetPrivateProfileInt - 从INI文件的指定Section 指定key 的整数值
GetPrivateProfileInt(LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址LPCTSTR lpKeyName, // 指向包含 Key 名称的字符串地址INT nDefault // 如果 Key 值没有找到,则返回缺省的值是多少LPCTSTR lpFileName // ini 文件的文件名);
2. GetPrivateProfileString - 从INI文件的指定Section 指定key 的串值
GetPrivateProfileString(LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址LPCTSTR lpKeyName, // 指向包含 Key 名称的字符串地址LPCTSTR lpDefault, // 如果 Key 值没有找到,则返回缺省的字符串的地址LPTSTR lpReturnedString, // 返回字符串的缓冲区地址DWORD nSize // 缓冲区的长度LPCTSTR lpFileName // ini 文件的文件名);
3. GetPrivateProfileSection - 从INI文件中读出指定Section 的内容
GetPrivateProfileSection(LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址LPTSTR lpReturnedString, // 返回数据的缓冲区地址DWORD nSize // 返回数据的缓冲区长度LPCTSTR lpFileName // ini 文件的文件名);
4. GetPrivateProfileSectionNames - 从INI文件中获得所有Section信息
GetPrivateProfileSectionNames(LPTSTR lpszReturnBuffer, // 返回数据的缓冲区地址DWORD nSize // 返回数据的缓冲区长度LPCTSTR lpFileName // ini 文件的文件名);
5. WritePrivateProfileSection - 将整个Section 的内容写入INI文件的指定Section中
WritePrivateProfileSection(LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址LPCTSTR lpString // 要写入的数据的地址LPCTSTR lpFileName // ini 文件的文件名);
6. WritePrivateProfileString - 将指定Key的串值写入INI文件的指定Section中
WritePrivateProfileString(LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址LPCTSTR lpKeyName, // 指向包含 Key 名称的字符串地址LPCTSTR lpString // 要写的字符串地址LPCTSTR lpFileName // ini 文件的文件名);
三.应用
在我们实际使用的时候,用的最多的是GetPrivateProfileString和WritePrivateProfileString,但在对自定义INI文件操作的时候要注意的是,如果lpFileName指定的文件不包含路径信息,API将默认为Windows 的安装目录而不是系统当前路径,比较方便的方法是使用.\ xxx.ini 作为文件名称指示API操作对象为当前路径下的xxx.ini文件。
(1)保存
void CRemoteMgrDlg::OnSave() { // TODO: Add your control notification handler code here CString IP_Save; char inBuf[256]; char path[255] ; m_CtrlIP.GetWindowText(IP_Save); if(strcmp(IP_Save,m_IPData)!=0) //IP地址有改变 { GetCurrentDirectory(256,inBuf); sprintf(path,"%s\\data.ini",inBuf); //设置ini文件的全路径 WritePrivateProfileString("信息","IP地址",IP_Save,path); strcpy(m_IPData,IP_Save); MessageBox("保存成功!","提示"); } }
(2)读取加载
CString CRemoteMgrDlg::Data_Loading() { char path[255] ; char inBuf[256]; CString IP_Save; GetCurrentDirectory(256,inBuf); sprintf(path,"%s\\data.ini",inBuf); //设置ini文件的全路径 GetPrivateProfileString("信息","IP地址","",IP_Save.GetBuffer(255),255,path); //读取数据 m_CtrlIP.SetWindowText(IP_Save); m_CtrlPort.SetWindowText("8888"); strcpy(m_IPData,IP_Save); return IP_Save;}
(3)删除节名下的内容
如果保存的数据比较多,并且每次保存的数据个数不定,可能会要求删除这个节名的内容,然后再进行数据的保存。
BOOL CRemoteMgrDlg::DelSection(LPCTSTR lpSection) { char inBuf[256]; char path[255] ; GetCurrentDirectory(256,inBuf); sprintf(path,"%s\\data.ini",inBuf); if(WritePrivateProfileString(lpSection,NULL,NULL,path)) return FALSE; else return GetLastError(); }
(4)具有相同性质的数组类数据写入
可以先写入这个数组的长度,然后for循环写入数组的内容
BOOL CIniFile::Write(LPCSTR AppName, LPCSTR KeyName, LONG RowIndex, LPCSTR lpString){ CHAR MKeyName[64]; sprintf(MKeyName,"%s_%ld",KeyName,RowIndex); return Write(AppName,MKeyName,lpString);}BOOL CIniFile::Write(LPCSTR AppName, LPCSTR KeyName, LPCSTR lpString){ return WritePrivateProfileString(AppName, KeyName, lpString,m_FileName);}
(4)具有相同性质的数组类数据读取
可以先读出这个数组的长度,然后for循环读出数组的内容
DWORD CIniFile::Read(LPCSTR AppName, LPCSTR KeyName, LONG RowIndex, LPCSTR lpDefault, LPSTR ReturnedString, DWORD nSize){ CHAR MKeyName[64]; sprintf(MKeyName,"%s_%ld",KeyName,RowIndex); return Read(AppName,MKeyName,lpDefault,ReturnedString,nSize);}DWORD CIniFile::Read(LPCSTR AppName, LPCSTR KeyName, LPCSTR lpDefault, LPSTR ReturnedString, DWORD nSize){ return GetPrivateProfileString(AppName,KeyName,lpDefault,ReturnedString,nSize,m_FileName); }
当然,对于数据的读写,也可以用普通文件的方式来完成。
- ini文件读取(C#)
- C#-读写INI文件
- C#读写INI文件
- C读写ini文件
- [c#]读写ini文件
- C读写ini文件
- C 读取ini文件
- 【C#】读取*.ini文件
- [c#]ini文件读写
- C#中读写INI文件
- c代码读取ini文件
- C#中读写INI文件
- 纯C读写ini文件
- C#INI文件操作类
- C/C++ INI文件操作
- Linux C 读取.ini文件
- INI文件的操作(ASP.NET+C#)
- 如何在C#中读写INI文件
- 艰难的选择
- C++成员函数的内存分配问题
- java集合框架【3】 java1.5新特性 ConcurrentHashMap、Collections.synchronizedMap、Hashtable讨论
- Lua脚本语言入门
- 怎么建立socket长连接???
- C++ INI文件
- 【转】linux下的C语言开发(自动编译工具)
- java thread自学笔记
- Source Insight技巧收集
- java 浅克隆与深克隆
- break与continue的用法区别
- aodv包头分析-上
- MSI安装包的修改经历
- poj 1007 Java