C++ INI文件
INI文件多用於存儲程序的初始化信息。例如,記錄程序連接數據庫的名稱、上一次用戶登錄的名稱、用戶的注冊信息等。
一. INI 文件格式
[Section1 Name] KeyName1=value1 KeyName2=value2 ...... ...... [Section2 Name] KeyName1=value1 KeyName2=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); }
當然,對於數據的讀寫,也可以用普通文件的方式來完成。