ini:很方便的一种文件格式,比较清晰明了的读写参数,方便的程序的运行。转自
配置文件中经常用到ini文件,在VC中其函数分别为:
写入.ini文件:
- BOOL WritePrivateProfileString(
- LPCTSTR lpAppName,
-
- LPCTSTR lpKeyName,
-
- LPCTSTR lpString,
-
- LPCTSTR lpFileName
- );
读取.ini文件:
- DWORD GetPrivateProfileString(
- LPCTSTR lpAppName,
-
- LPCTSTR lpKeyName,
-
- LPCTSTR lpDefault,
-
- LPTSTR lpReturnedString,
-
- DWORD nSize,
-
- LPCTSTR lpFileName
- );
读取整形值:(返回值为读到的整)
- UINT GetPrivateProfileInt(
- LPCTSTR lpAppName,
- LPCTSTR lpKeyName,
- INT nDefault,
-
- LPCTSTR lpFileName
-
- );
读写INI文件时相对路径和绝对路径都可以,根据实际情况选择 (我自己实际使用当中并不能使用相对路径,麻烦高手提点一下)
"..\\IniFileName.ini" // 这样的为相对路径
"D:\\IniFileName.ini" // 这样的为绝对路径
MAX_PATH:是微软最大路径占的字节所设的宏
例子:
写INI文件:
- LPTSTR lpPath = new char[MAX_PATH];
-
- strcpy(lpPath, "D:\\IniFileName.ini");
-
- WritePrivateProfileString("LiMing", "Sex", "Man", lpPath);
- WritePrivateProfileString("LiMing", "Age", "20", lpPath);
-
- WritePrivateProfileString("Fangfang", "Sex", "Woman", lpPath);
- WritePrivateProfileString("Fangfang", "Age", "21", lpPath);
-
-
-
- delete [] lpPath;
-
-
- INI文件如下:
-
- [LiMing]
- Sex=Man
- Age=20
- [Fangfang]
- Sex=Woman
- Age=21
-
- 读INI文件:
-
-
-
- LPTSTR lpPath = new char[MAX_PATH];
- LPTSTR LiMingSex = new char[6];
- int LiMingAge;
- LPTSTR FangfangSex = new char[6];
- int FangfangAge;
-
-
- strcpy(lpPath, "..\\IniFileName.ini");
-
- GetPrivateProfileString("LiMing", "Sex", "", LiMingSex, 6, lpPath);
- LiMingAge = GetPrivateProfileInt("LiMing", "Age", 0, lpPath);
-
- GetPrivateProfileString("Fangfang", "Sex", "", FangfangSex, 6, lpPath);
- FangfangAge = GetPrivateProfileInt("Fangfang", "Age", 0, lpPath);
-
-
-
- delete [] lpPath;
以上