我建了一个基于DIALOG的应用程序CDemoApp
BOOL CTitlekeyEntryApp::InitInstance()
{
char iniName[_MAX_PATH];
::_snprintf(iniName, sizeof(iniName), "%s\\Demo.INI", "C:\demo") );free((void*)m_pszProfileName);
m_pszProfileName=_tcsdup(_T(iniName));
CString str = this->GetProfileString("LOGPARAM","FILENAME","aaa.log");
}
Demo.INI 文件内容如下
[LOGPARAM]
DIR =C:\
FILENAME =other.log
MAXSIZE =1024000
HISTORY =2
为什么str=aaa.log,不能从Demo.INI 中读出FILENAME other.log?
BOOL CTitlekeyEntryApp::InitInstance()
{
char iniName[_MAX_PATH];
::_snprintf(iniName, sizeof(iniName), "%s\\Demo.INI", "C:\demo") );free((void*)m_pszProfileName);
m_pszProfileName=_tcsdup(_T(iniName));
CString str = this->GetProfileString("LOGPARAM","FILENAME","aaa.log");
}
Demo.INI 文件内容如下
[LOGPARAM]
DIR =C:\
FILENAME =other.log
MAXSIZE =1024000
HISTORY =2
为什么str=aaa.log,不能从Demo.INI 中读出FILENAME other.log?
In Windows 3.x, the value is stored in the WIN.INI file.
In Windows 95, the value is stored in a cached version of WIN.INI
环境:VC6, XP, W2K, Win9X, Win95, NT4, MFCsoarlove说明,由于翻译的可能和你的平时叫法不同,举个例子说明下面所说的片段名,键名,键值具体含义:[Version]
Signature="$Windows NT$"
Version:片段名
Signature:键名
"$Windows NT$":键值下面是翻译的作者的说明。介绍CIniReader是一个读写ini文件的类,下面是这个类的功能列表:setINIFileName 要读写的ini文件名称
getKeyValue 得到给定片段名和键名的键值
getSectionData 得到给定片段的所有键名/键值
getSectionNames 得到给定几个片段的所有键名/键值
sectionExists 判断是否有片段存在
setKey 添加或设置一对键名和键值CIniReader和GetPrivateProfilexxx ()函数用来设置或得到一个ini文件的存储信息。下面是CIniReader的头文件
//
// CIniReader- Header File
//
class CIniReader
{
public:
// method to set INI file name, if not already specified
void setINIFileName(CString strINIFile);// methods to return the lists of section data and section names
CStringList* getSectionData(CString strSection);
CStringList* getSectionNames();// check if the section exists in the file
BOOL sectionExists(CString strSection);// updates the key value, if key already exists, else creates a key-value pair
long setKey(CString strValue, CString strKey, CString strSection);// give the key value for the specified key of a section
CString getKeyValue(CString strKey,CString strSection);// default constructor
CIniReader()
{
m_sectionList = new CStringList();
m_sectionDataList = new CStringList();
}CIniReader(CString strFile)
{
m_strFileName = strFile;
m_sectionList = new CStringList();
m_sectionDataList = new CStringList();
}~CIniReader()
{
delete m_sectionList;
delete m_sectionDataList;
}private:
// lists to keep sections and section data
CStringList *m_sectionDataList;
CStringList *m_sectionList;CString m_strSection;
long m_lRetValue;// ini file name
CString m_strFileName;
};怎样使用这个类示例工程显示了怎样使用这个类。在INI File name edit box中添加ini文件名C:\WINDOWS\ODBC.INI,按List Sections按钮得到ini文件的片段列表。指定任何一个片段名称,按List Keys按钮,将得到所有的键名和对应键值的列表。指定一个键名,按Show Key Value按钮将显示对应此键名的键值。Update Key Value将修改你的键值,如果你不知道你在干什么,请不要做,因为这会改变你的ini文件。目前缓冲区限制在2000字符,你可以根据你的需要增加。例子代码:
// example of using the setINIFileName method
CIniReader m_IniReader;
m_strINIFile = “C:\\WINDOWS\\ODBC.INI”;
m_IniReader.setINIFileName (m_strINIFile);
In Windows 3.x, the value is stored in the WIN.INI file.
In Windows 95, the value is stored in a cached version of WIN.INI为什么不可以呢