.ini文件是一种很方便的文件格式,可以清晰明了的读写参数,方便的程序的运行。
写入.ini文件:
BOOL WritePrivateProfileString
(
LPCTSTR lpAppName, // INI文件中的一个字段名[节名]可以有很多个节名
LPCTSTR lpKeyName, // lpAppName 下的一个键名,也就是里面具体的变量名
LPCTSTR lpString, // 键值,也就是数据
LPCTSTR lpFileName // INI文件的路径
);
读取.ini文件:
DWORD GetPrivateProfileString
(
LPCTSTR lpAppName, // INI文件中的一个字段名[节名]可以有很多个节名
LPCTSTR lpKeyName, // lpAppName 下的一个键名,也就是里面具体的变量名
LPCTSTR lpDefault, // 如果lpReturnedString为空,则把个变量赋给lpReturnedString
LPTSTR lpReturnedString, // 存放键值的指针变量,用于接收INI文件中键值(数据)的接收缓冲区
DWORD nSize, // lpReturnedString的缓冲区大小
LPCTSTR lpFileName // INI文件的路径
);
读取整形值:(返回值为读到的整)
UINT GetPrivateProfileInt
(
LPCTSTR lpAppName, // INI文件中的一个字段名[节名]可以有很多个节名
LPCTSTR lpKeyName, // lpAppName 下的一个键名,也就是里面具体的变量名
INT nDefault, // 如果没有找到指定的数据返回,则把个变量值赋给返回值
LPCTSTR lpFileName // INI文件的路径
);
读写INI文件时相对路径和绝对路径都可以,根据实际情况选择
"..\\IniFileName.ini" // 这样的为相对路径
"D:\\IniFileName.ini" // 这样的为绝对路径
例子:读写INI文件
CString admin; //账户
CString String_path = _T("d:\\UserMessage.ini"); //登录账户的配置文件路径
::WritePrivateProfileString(_T("SaveInfo"),_T("用户名"),admin,String_path);//写配置文件
::GetPrivateProfileString(_T("SaveInfo"),_T("用户名"),_T("admin"),admin.GetBuffer(MAX_PATH),MAX_PATH,String_path);//读配置文件
admin.ReleaseBuffer();