ini檔案的c++讀寫方式
ini檔案的c++讀寫方式
檔案例項
檔名稱:student.ini
[student]
name = 小李
sex = 男
age = 16
函式簡介
1、寫入.ini檔案:
BOOL WritePrivateProfileString(
LPCTSTR lpAppName, // INI檔案中的一個欄位名[節名]可以有很多個節名,如上[student]
LPCTSTR lpKeyName, // lpAppName 下的變數名,如上name
LPCTSTR lpString, // lpAppName 下的變數對應的值,如小李
LPCTSTR lpFileName // INI檔案的路徑
2、讀取.ini檔案:
DWORD GetPrivateProfileString(
LPCTSTR lpAppName, // INI檔案中的一個欄位名[節名]可以有很多個節名,如上[student]
LPCTSTR lpKeyName, // lpAppName 下的變數名,如上name
LPCTSTR lpDefault, // 如果lpReturnedString為空,則把個變數賦給lpReturnedString
LPTSTR lpReturnedString, //存放 INI檔案中鍵值(資料),如小李
DWORD nSize, // lpReturnedString的緩衝區大小
LPCTSTR lpFileName // INI檔案的路徑
3、讀取整形值:(返回值為讀到的整)
UINT GetPrivateProfileInt(
LPCTSTR lpAppName, // INI檔案中的一個欄位名[節名]可以有很多個節名,如上[student]
LPCTSTR lpKeyName, // lpAppName 下的變數名,如上age
INT nDefault, // 如果沒有找到指定的資料返回,則把個變數值賦給返回值
LPCTSTR lpFileName // INI檔案的路徑
);
以上為寫入和讀取的函式介面,比較簡單沒什麼好說的,正常使用即可。
使用注意事項(檔案路徑)
1、引數中的檔案路徑可寫絕對路徑和相對路徑;
2、絕對路徑沒啥說的,直接寫即可;
如:兩者都在bin路徑下,不能直接寫成.\xxx.ini或者xxx.ini。需要寫成…\bin\xxx.ini